How to check if value is 'null'

G

GS

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
 
B

Brian Lin

GS said:
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 ?
 
G

GS

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

Luke Dalessandro

GS said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top