Check if an object is null

Discussion in 'ASP .Net' started by shapper, Oct 9, 2006.

  1. shapper

    shapper Guest

    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
     
    shapper, Oct 9, 2006
    #1
    1. Advertising

  2. Try DBNull.value.


    --
    Ameet Phadnis
    Sr. Technical Consultant
    e Tek Global Inc.
    ASP Alliance Author Page: http://aspalliance.com/author.aspx?uId=44260


    "shapper" wrote:

    > 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
    >
    >
     
    =?Utf-8?B?QW1lZXQgUGhhZG5pcyhlIFRlayBHbG9iYWwgSW5j, Oct 9, 2006
    #2
    1. Advertising

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


    --
    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006


    "shapper" <> wrote in message
    news:...
    > 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
    >
     
    Mark Fitzpatrick, Oct 9, 2006
    #3
  4. 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!
    *************************************************
    "shapper" <> wrote in message
    news:...
    > 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
    >
     
    Cowboy \(Gregory A. Beamer\), Oct 10, 2006
    #4
  5. shapper

    shapper Guest

    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

    Cowboy (Gregory A. Beamer) wrote:
    > 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!
    > *************************************************
    > "shapper" <> wrote in message
    > news:...
    > > 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
    > >
     
    shapper, Oct 10, 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. Replies:
    16
    Views:
    7,341
    Mike Schilling
    Oct 12, 2005
  2. Replies:
    5
    Views:
    26,691
    Mike Schilling
    Mar 29, 2006
  3. G Fernandes
    Replies:
    9
    Views:
    597
    DHOLLINGSWORTH2
    Feb 27, 2005
  4. Cirene
    Replies:
    1
    Views:
    815
    Alexey Smirnov
    Jun 9, 2008
  5. Mike
    Replies:
    12
    Views:
    716
    Daniel Pitts
    Sep 26, 2008
Loading...

Share This Page