By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

Check for existence of an object?

P: n/a
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


Mar 21 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <c_**********@hotmail.com> wrote in message
news:uQ*****************@newssvr27.news.prodigy.ne t...
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

Mar 21 '06 #2

P: n/a
"CK" <c_**********@hotmail.com> 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.
Mar 21 '06 #3

P: n/a
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" <fi******************@CL4.org> wrote in message
news:dv**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
"CK" <c_**********@hotmail.com> 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.

Mar 21 '06 #4

P: n/a

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
Mar 21 '06 #5

P: n/a
> 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
Mar 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.