Nullable strings as parameters

Discussion in 'ASP .Net' started by Mark Rae, Jan 25, 2006.

  1. Mark Rae

    Mark Rae Guest

    Hi,

    In v1.1 I used to simulate nullable datatypes with structs. e.g.

    public struct NullableString
    {
    private readonly String pstrValue;
    private readonly bool blnHasValue;
    public NullableString(String strValue)
    {
    this.pstrValue = strValue;
    this.blnHasValue = true;
    }
    public String Value {get{return this.pstrValue;}}
    public bool HasValue {get{return blnHasValue;}}
    };

    However, now that we have these natively in v2, I'm trying to use them as
    part of my database abstraction layer. I have a class which begins:

    public class CSale
    {
    private String? pstrTransactionID;
    public String? strTransactionID {get{return
    pstrTransactionID;}set{pstrTransactionID = value;}}

    private DateTime? pdtmPayment;
    public DateTime? dtmPayment{get{return pdtmPayment;}set{pdtmPayment =
    value;}}

    private int? pintItems;
    public int? intItems {get{return pintItems;}set{pintItems = value;}}

    private decimal? pcurFee;
    public decimal? curFee{get{return pcurFee;}set{pcurFee = value;}}

    // etc
    }

    However, this doesn't compile. Instead, the compiler generates the following
    error:
    The type 'string' must be a non-nullable value type in order to use it as
    parameter 'T' in the generic type or method 'System.Nullable<T>'

    IntelliSense still works, though, i.e. if I type a period after
    strTransactionID, it drops down the list of properties and methods e.g.
    HasValue, Value etc.

    If I comment out the two lines relating to the String parameter, the class
    compiles OK.

    Any assistance gratefully received.

    Mark
    Mark Rae, Jan 25, 2006
    #1
    1. Advertising

  2. > However, this doesn't compile. Instead, the compiler generates the following
    > error:
    > The type 'string' must be a non-nullable value type in order to use it as
    > parameter 'T' in the generic type or method 'System.Nullable<T>'


    Only value types (i.e. structs) can be made nullable (as these cannot
    otherwise be set to 'null'); it is rather meaningless to use string? as
    string is a reference type anyway, so you can just set a string object
    to null...so, just don't make your string members nullable explicitly,
    and you'll still be able to assign them to null as you require.
    Paul Henderson, Jan 25, 2006
    #2
    1. Advertising

  3. Mark Rae

    Mark Rae Guest

    "Paul Henderson" <> wrote in message
    news:...

    > Only value types (i.e. structs) can be made nullable (as these cannot
    > otherwise be set to 'null'); it is rather meaningless to use string? as
    > string is a reference type anyway, so you can just set a string object
    > to null...so, just don't make your string members nullable explicitly,
    > and you'll still be able to assign them to null as you require.


    I see.

    For the other various datatypes, I use variations of the following code to
    build up a SqlParameters collection:

    mobjSqlParameter = new SqlParameter("@pdtmSale", SqlDbType.SmallDateTime);
    mobjSqlParameter.IsNullable = true;
    mobjSqlParameter.Value = dtmSale.HasValue ? dtmSale.Value :
    (object)DBNull.Value;
    maobjSqlParameters.Add(mobjSqlParameter);

    Am I able to do something similar with strings?
    Mark Rae, Jan 25, 2006
    #3
  4. > For the other various datatypes, I use variations of the following code to
    > build up a SqlParameters collection:
    >
    > mobjSqlParameter = new SqlParameter("@pdtmSale", SqlDbType.SmallDateTime);
    > mobjSqlParameter.IsNullable = true;
    > mobjSqlParameter.Value = dtmSale.HasValue ? dtmSale.Value :
    > (object)DBNull.Value;
    > maobjSqlParameters.Add(mobjSqlParameter);
    >
    > Am I able to do something similar with strings?


    Yes; for the third line you should just be able to use:
    mobjSqlParameter.Value = myStr == null ? (object)DBNull.Value :
    myStr;
    Paul Henderson, Jan 25, 2006
    #4
  5. Mark Rae

    Mark Rae Guest

    "Paul Henderson" <> wrote in message
    news:...

    >> Am I able to do something similar with strings?

    >
    > Yes; for the third line you should just be able to use:
    > mobjSqlParameter.Value = myStr == null ? (object)DBNull.Value :
    > myStr;


    Cool! Thanks a lot for your assistance.
    Mark Rae, Jan 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. buran

    nullable sqlparameter

    buran, Jul 17, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    516
    buran
    Jul 17, 2003
  2. Kevin Yu

    DateTime not nullable problem

    Kevin Yu, Jan 28, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    7,634
    MWells
    Jan 29, 2005
  3. Jacques Wentworth

    Property 'Keycode' is non-nullable

    Jacques Wentworth, Mar 11, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,613
    Jacques Wentworth
    Mar 11, 2005
  4. Child

    Nullable DateTime variables

    Child, May 3, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    24,593
    elizas
    May 7, 2010
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    756
    Malcolm
    Jun 24, 2006
Loading...

Share This Page