How to check if value is 'null'

Discussion in 'ASP .Net' started by GS, Jan 15, 2006.

  1. GS

    GS Guest

    Hi,

    I have following value which I need to check wether it's null or not. Issue is that StartPrice itself can be a null and checking (myItem.StartPrice.Value == null) do not produce true.
    How do I check for null in this kind of case. I don't want to check StartPrice for null and then Value for null again as one option. I just want to check Value for null.
    Thanks,
    Greg
    GS, Jan 15, 2006
    #1
    1. Advertising

  2. GS

    Brian Lin Guest

    GS wrote:
    > Hi,
    >
    > I have following value which I need to check wether it's null or not.
    > Issue is that StartPrice itself can be a null and checking
    > (myItem.StartPrice.Value == null) do not produce true.
    > How do I check for null in this kind of case. I don't want to check
    > StartPrice for null and then Value for null again as one option. I just
    > want to check Value for null.
    >
    > Thanks,
    > Greg
    >

    myItem.StartPrice.Value IS Nothing ?
    Brian Lin, Jan 15, 2006
    #2
    1. Advertising

  3. GS

    GS Guest

    Does not work since myItem.StartPrice is null (Nothing in VB) itself and
    hence I can not check myItem.StartPrice.Value

    "Brian Lin" <> wrote in message
    news:...
    > GS wrote:
    >> Hi,
    >> I have following value which I need to check wether it's null or not.
    >> Issue is that StartPrice itself can be a null and checking
    >> (myItem.StartPrice.Value == null) do not produce true.
    >> How do I check for null in this kind of case. I don't want to check
    >> StartPrice for null and then Value for null again as one option. I just
    >> want to check Value for null.
    >>
    >> Thanks,
    >> Greg
    >>

    > myItem.StartPrice.Value IS Nothing ?
    GS, Jan 15, 2006
    #3
  4. GS wrote:
    > Hi,
    >
    > I have following value which I need to check wether it's null or not.
    > Issue is that StartPrice itself can be a null and checking
    > (myItem.StartPrice.Value == null) do not produce true.
    > How do I check for null in this kind of case. I don't want to check
    > StartPrice for null and then Value for null again as one option. I just
    > want to check Value for null.
    >
    > Thanks,
    > Greg
    >

    Just do this:

    if ((null == myItem.StartPrice) || (null == myItem.StartPrice.Value))
    {
    // Either .StartPrice or .Value is null, handle that here
    }
    else
    {
    // Do stuff with .Value
    }

    The second test is never evaluated if the first test is true (it's
    "short-circuited"), so you won't get an exception. Keep in mind that you
    might need to use a different test on the .Value check if .Value is an
    int or a string.

    Hope this helps,
    Luke
    Luke Dalessandro, Jan 15, 2006
    #4
    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. Brett
    Replies:
    15
    Views:
    151,687
    sumanvarma
    Mar 31, 2008
  2. Replies:
    5
    Views:
    26,620
    Mike Schilling
    Mar 29, 2006
  3. G Fernandes
    Replies:
    9
    Views:
    591
    DHOLLINGSWORTH2
    Feb 27, 2005
  4. Col
    Replies:
    1
    Views:
    624
    peter koch
    Apr 21, 2006
  5. Mike
    Replies:
    12
    Views:
    712
    Daniel Pitts
    Sep 26, 2008
Loading...

Share This Page