Testing for an object

  • Thread starter Thread starter Lance
  • Start date Start date
L

Lance

Hi,

How do you test for an object existing in C#? C# seems to differentiate
between 'null' and non-existance. In other languages I could test as so:

if(ex.InnerException.StackTrace != null)

nvc.Add("** INNER EXCEPTION", ex.InnerException.StackTrace.ToString());

But an exception gets thrown at the if(...) statement. 'Object Reference
not set to an Instance of an Object'.

So how can I test for an object's existance?

Thanks,
Lance
 
Lance said:
How do you test for an object existing in C#? C# seems to differentiate
between 'null' and non-existance. In other languages I could test as so:

if(ex.InnerException.StackTrace != null)

nvc.Add("** INNER EXCEPTION", ex.InnerException.StackTrace.ToString());

But an exception gets thrown at the if(...) statement. 'Object Reference
not set to an Instance of an Object'.

So how can I test for an object's existance?

Firstly, you're not testing for an object's existence - you're testing
whether or not the value of a variable is a reference to an object, or
whether it's null.

Secondly, the NullReferenceException you got above is almost certainly
due to ex.InnerException being null - if there's no inner exception,
you have a problem trying to get at its stack trace! You'd have the
same problem in most other languages.
 
Lance said:
Hi,

How do you test for an object existing in C#? C# seems to differentiate
between 'null' and non-existance. In other languages I could test as so:

if(ex.InnerException.StackTrace != null)

nvc.Add("** INNER EXCEPTION", ex.InnerException.StackTrace.ToString());

But an exception gets thrown at the if(...) statement. 'Object Reference
not set to an Instance of an Object'.

So how can I test for an object's existance?

Thanks,
Lance

You CAN use this, but (in this case) ONLY if ex.InnerException exists ...

so it is:
if (ex.InnerException != null && ex.InnerException.StackTrace != null)
{
....
}


Hans Kesting
 
Ok, fair enough, but how do I test for it? I tried breaking up the test, as
I had a theory that perhaps if the InnerException was missing the StackTrace
would be missing in a worse(!?) way. On the following code the exception
still gets thrown at the first if(...)


if(ex.InnerException != null)

if(ex.InnerException.StackTrace != null)

nvc.Add("** INNER EXCEPTION", ex.InnerException.StackTrace.ToString());
 
Lance said:
Ok, fair enough, but how do I test for it? I tried breaking up the test, as
I had a theory that perhaps if the InnerException was missing the StackTrace
would be missing in a worse(!?) way.

This is why you need to review what you mean by "missing". The value of
an expression can't be missing - but it can be null, in which case, if
you try to dereference that value (eg to try to use a property) you'll
get a NullReferenceException.
On the following code the exception
still gets thrown at the first if(...)

if(ex.InnerException != null)

if(ex.InnerException.StackTrace != null)

nvc.Add("** INNER EXCEPTION", ex.InnerException.StackTrace.ToString());

If the exception is still getting thrown, that suggests that ex itself
is null.
 
Thanks for the quick replies.

Assume that 'ex' is already instatiated.

I'd like to know how to *test* for the existance of a value(or an object).
I understand that if the value is empty, I can't use it. So how do I *test*
for it's existance. I am hoping to use some function like:

System.Test(object)

as in

if(System.Test(object) == true)...

or even

if(System.Test(object.property) == true)...

to see if the object (or property) exists. If the object is instantiated,
it returns true, if not false. Maybe something in the Reflection namespace
will do this?
 
Lance said:
Thanks for the quick replies.

Assume that 'ex' is already instatiated.

ex isn't an object, it's a variable. I find terminology terribly
important in this kind of discussion. In particular, if ex is non-null,
then

if (ex.InnerException==null)

shouldn't throw a NullReferenceException (assuming ex.InnerException is
reasonably implemented).
I'd like to know how to *test* for the existance of a value(or an object).

There's no such concept as a non-existent object. What you *can* test
is whether the value of a reference type expression is null, which you
do precisely with

if (expression==null)

You really need to get away from the idea that expressions *are*
objects. At most, the value of an expression is a reference to an
object.
 
Back
Top