Check for existence of an object?

C

CK

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
 
K

Kevin Spencer

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

Paul E Collins

CK said:
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.
 
C

CK

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 said:
CK said:
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.
 
L

Lars-Inge Tønnessen \(VJ# MVP\)

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
 
L

Lars-Inge Tønnessen \(VJ# MVP\)

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top