Check if an object is null

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
 
M

Mark Fitzpatrick

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.
 
C

Cowboy \(Gregory A. Beamer\)

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!
*************************************************
 
S

shapper

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
 

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

Similar Threads

Check if it is null 1
Default value 1
Property in Class 1
Default property value 4
how check if session is null? 1
what do i do if it's null 11
Null and Generic Type 2
Create user 1

Top