Check for existence of an object?

Discussion in 'ASP .Net' started by CK, Mar 21, 2006.

  1. CK

    CK Guest

    How can I check for the exisitence of an object in C#/Asp.net?
    I have a routine that initailizes form values based on a loaded up object,
    however if the object hasn't been created I get an Object reference not set
    to an instance of an object. Is there a function I can use to see if the
    object exists before trying to use it properties?

    Thanks for any advice,
    Chris
     
    CK, Mar 21, 2006
    #1
    1. Advertising

  2. if (objectName != null) ...

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Professional Numbskull

    Show me your certification without works,
    and I'll show my certification
    *by* my works.

    "CK" <> wrote in message
    news:uQXTf.3712$...
    > How can I check for the exisitence of an object in C#/Asp.net?
    > I have a routine that initailizes form values based on a loaded up object,
    > however if the object hasn't been created I get an Object reference not
    > set to an instance of an object. Is there a function I can use to see if
    > the object exists before trying to use it properties?
    >
    > Thanks for any advice,
    > Chris
    >
    >
    >
    >
     
    Kevin Spencer, Mar 21, 2006
    #2
    1. Advertising

  3. "CK" <> wrote:

    > How can I check for the exisitence of an object in
    > C#/Asp.net? [...] if the object hasn't been created
    > I get an Object reference not set to an instance of
    > an object.


    Usually, supposing your object is called "o", you'd just test (o ==
    null).

    Eq.
     
    Paul E Collins, Mar 21, 2006
    #3
  4. CK

    CK Guest

    Thanks Guys. I am an idiot but I did figure it out.
    I am used to VB. They had an IsNull, IsNothing, and IsObject functions.


    "Paul E Collins" <> wrote in message
    news:dvpk0i$d2t$-infra.bt.com...
    > "CK" <> wrote:
    >
    >> How can I check for the exisitence of an object in
    >> C#/Asp.net? [...] if the object hasn't been created
    >> I get an Object reference not set to an instance of
    >> an object.

    >
    > Usually, supposing your object is called "o", you'd just test (o == null).
    >
    > Eq.
    >
    >
     
    CK, Mar 21, 2006
    #4
  5. if ( YourObject == null ) return; // Object does not exists. Jump out of
    function.
    // Statemens if the object exists...


    or

    if ( YourObject != null )
    {
    // The object exists, do statemenst.
    }

    Regards,
    Lars-Inge Tønnessen
     
    Lars-Inge Tønnessen \(VJ# MVP\), Mar 21, 2006
    #5
  6. > I am used to VB.

    if not YourObject is Nothing Then
    // Do VB.NET statements... Your object exists...
    End If


    Regards,
    Lars-Inge Tønnessen
     
    Lars-Inge Tønnessen \(VJ# MVP\), Mar 21, 2006
    #6
    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. Leon
    Replies:
    1
    Views:
    433
    John Timney \(Microsoft MVP\)
    Oct 18, 2003
  2. TaeHo Yoo
    Replies:
    3
    Views:
    5,961
    John Timney \(Microsoft MVP\)
    Oct 20, 2003
  3. Replies:
    3
    Views:
    2,943
    Gordon Beaton
    Apr 17, 2006
  4. Thomas Scheffler

    XSLT: check existence of a variable

    Thomas Scheffler, Sep 17, 2003, in forum: XML
    Replies:
    0
    Views:
    693
    Thomas Scheffler
    Sep 17, 2003
  5. Cory Patterson
    Replies:
    4
    Views:
    129
    Robert Klemme
    Aug 19, 2010
Loading...

Share This Page