locked
How to check if an object is null

    Question

  • Hi,

    I can't figure out how to check if an object is null. I am writing a simple VB.NET program to interface with some other VB.NET code that I did not write. Here is an example of what I am doing:

    Dim annualReview As jAnnualReview

    annualReview = CW.Get_Annual_Review(token, clientPK, year, domain_pk)

    Sometimes an annualReview does not exist, and in the debugger the value of annualReview is "Nothing". How do I test for this? I've tried the usual and the editor doesn't like any of it:

    If annualReview = "" 
    If annualReview = 0
    If annualReview = NULL
    If annualReview = "Nothing"

    Thanks for you help,
    Kathy


    Tuesday, November 08, 2005 9:06 PM

Answers


  • if (MyObject = DbNull.value)

    Must be an object though...no other types work...

    If IsNothing(MyType) then

    and this IsNull function from VB6 has changed to IsDbNull

    The System.DBNull value indicates that the Object represents missing or nonexistent data. DBNull is not the same as Nothing, which indicates that a variable has not yet been initialized. DBNull is also not the same as a zero-length string (""), which is sometimes referred to as a null string.

    Wednesday, November 09, 2005 6:35 PM
    Moderator

All replies

  • yeah. . .one of the things that makes VB an inferior language is its hijacking of standard keywords for its bassackwards methodology. GRRRRR!!!

    In virtually every real OO language, the 'is' keyword does a type comparison. . . but in VB.NOT (as well as VBSUX) 'is' does a reference comparison. So, what you want to do is compare an object reference to the 'Nothing'  reference -
     
    if MyObj is nothing then
       Debug.Print "MyObj not initialized"
    end if

    why VB doesn't use null is a mystery to me! oh yeah its because VB sux!!!
    Tuesday, November 08, 2005 10:33 PM
    Moderator

  • if (MyObject = DbNull.value)

    Must be an object though...no other types work...

    If IsNothing(MyType) then

    and this IsNull function from VB6 has changed to IsDbNull

    The System.DBNull value indicates that the Object represents missing or nonexistent data. DBNull is not the same as Nothing, which indicates that a variable has not yet been initialized. DBNull is also not the same as a zero-length string (""), which is sometimes referred to as a null string.

    Wednesday, November 09, 2005 6:35 PM
    Moderator
  • Thank you very much. Just what I needed!

    Kathy
    Wednesday, November 09, 2005 6:57 PM