473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Testing for an object

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
Nov 16 '05 #1
6 1941
Lance <lance@[nospam]keayweb.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

"Lance" <lance@[nospam]keayweb.com> wrote in message news:10*************@corp.supernews.com...
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
Nov 16 '05 #3
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());

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Lance <lance@[nospam]keayweb.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Lance <lance@[nospam]keayweb.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
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?
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Lance <lance@[nospam]keayweb.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
Lance <lance@[nospam]keayweb.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is...
21
by: scandal | last post by:
I am a javascript newbie working on a script that checks whether a "path" from one element in an array to another is "blocked." Currently, the script pushes an already processed cell index (hence...
38
by: Christoph Zwerschke | last post by:
In August 2001, there was a thread about the "Art of Unit Testing": http://groups.google.com/group/comp.lang.python/browse_frm/thread/aa2bd17e7f995d05/71a29faf0a0485d5 Paul Moore asked the...
5
by: Brian | last post by:
Hello all.. Am working on an Air Hockey game... have an table loaded into a picture box. The borders of the table are slightly slanted. Am using hit testing lines with GDI+ to manipulate the...
2
by: Naveen Mukkelli | last post by:
Hi, I'm writing a client/server application using C#. The server accepts connecitons asynchronously, using BeginAccept/EndAccept. Is there any way we can write some unit tests(NUnit) to test...
15
by: Enrique | last post by:
Question I am posting this question again (3rd time) because some issues with my no spam alias. Here it is the question: I have not been able to run unit tests for a VSTO (2005) project. I...
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
2
by: Mark Elliott | last post by:
Hi All, I'm just playing with asp.net mvc and link-to-sql for the first time. I'm using visual web developer 2008. I create the project fine, choose nunit as my test framework in the wizard. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.