Check if an object is null

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

Inside a class I have 4 propertiy types:

1. String

2. Boolean

3. Mail.MailPriority

4. Mail.MailAddressCollection

I need to check if each property is null, i.e., if it wasn't defined
any value for them.

How can I do it?

I tried NULL but it got an error to use System.DBNull but I got error
again.

Thanks,

Miguel
 
You would use the System.DBNull.Value as a null test if you're pulling data
from a datareader or other database source. Also with a string it could be
initialised but empty, which you may also want to handle differently by
testing if it's a String.Empty.
 
In ASP.NEt 1.1 or in 2.0 without a nullable type, you would use DbNull.Value

if (x == DbNull.Value)
{
//is null
}

In 2.0, you have nullable types. I assume you are not using them however, or
are using VB.NET. In VB.NET 2.0, you use

Dim nullable1 As Nullable(Of DateTime) = New Nullable(Of DateTime)
If Not nullable1.HasValue Then
End If


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
 
I am using:

If MyObject.Equals(DBNull.Value) Then
....

Do you think this work?

At least I didn't have any problem in my code.

Thanks,
Miguel
 
Back
Top