473,804 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.InnerExce ption.StackTrac e != null)

nvc.Add("** INNER EXCEPTION", ex.InnerExcepti on.StackTrace.T oString());

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 1959
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.InnerExce ption.StackTrac e != null)

nvc.Add("** INNER EXCEPTION", ex.InnerExcepti on.StackTrace.T oString());

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 NullReferenceEx ception you got above is almost certainly
due to ex.InnerExcepti on 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.co m>
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.supe rnews.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.InnerExce ption.StackTrac e != null)

nvc.Add("** INNER EXCEPTION", ex.InnerExcepti on.StackTrace.T oString());

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.InnerExcepti on exists ...

so it is:
if (ex.InnerExcept ion != null && ex.InnerExcepti on.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.InnerExce ption != null)

if(ex.InnerExce ption.StackTrac e != null)

nvc.Add("** INNER EXCEPTION", ex.InnerExcepti on.StackTrace.T oString());

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.InnerExce ption.StackTrac e != null)

nvc.Add("** INNER EXCEPTION", ex.InnerExcepti on.StackTrace.T oString());

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 NullReferenceEx ception you got above is almost certainly
due to ex.InnerExcepti on 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.co m>
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 NullReferenceEx ception.
On the following code the exception
still gets thrown at the first if(...)

if(ex.InnerExce ption != null)

if(ex.InnerExce ption.StackTrac e != null)

nvc.Add("** INNER EXCEPTION", ex.InnerExcepti on.StackTrace.T oString());


If the exception is still getting thrown, that suggests that ex itself
is null.

--
Jon Skeet - <sk***@pobox.co m>
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(obj ect)

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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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 NullReferenceEx ception.
On the following code the exception
still gets thrown at the first if(...)

if(ex.InnerExce ption != null)

if(ex.InnerExce ption.StackTrac e != null)

nvc.Add("** INNER EXCEPTION",

ex.InnerExcepti on.StackTrace.T oString());
If the exception is still getting thrown, that suggests that ex itself
is null.

--
Jon Skeet - <sk***@pobox.co m>
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.InnerExcept ion==null)

shouldn't throw a NullReferenceEx ception (assuming ex.InnerExcepti on 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==nu ll)

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.co m>
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
3743
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 possible). Most of my programming these days involves using PHP for creating script files for automating tasks and procedures (locally), and also for anything that might be needed by our divisional Intranet (not a huge site by any stretch of the...
21
21228
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 an integer) into an array. To prevent rechecking already processed cells, the script iterates through the (sorted) array to see whether that integer is an element of the array. After reading about javascript arrays a bit more, I thought...
38
3416
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 legitimate question why there is no hook for a "global" fixture code that is run only once for the whole TestCase, as opposed to the normal "setUp" and "tearDown" code that is run for every single test in the TestCase. A "global fixture" would be...
5
2737
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 puck moving around. I want the puck is bounce when it hits a border (specified by the hitlines). Retreieved some info on hit testing lines from Bob Powell's GDI+ FAQ (very useful!) but i'm fairly new at the idea of hit testing and am
2
3223
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 the behaviour of accepting connections and testing some other private methods that would be called when the server receives a connection request.
15
6963
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 have an Excel project and a unit test project in the same solution. I have not found the way to initialize the runtimecallback as required by the code generated
18
2402
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 that great. One of the cons of ASP & Javascript is that they're both interpreted, which means one has twice the amount of work to do interms of syntax checking & semantic/runtime checking. Another bad thing is that ASP & Javascript doesn't have...
11
2385
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 realized that despite suggestions to use DHTML-based modal dialogs are very common? there is not a single fully functional reliable copyright-free cross-browser alternative to say MsgBox (VBScript) or showModalDialog (IE). This way such suggestions up to...
2
2244
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. Everything is fine, compiles and the tests work fine on the basic application. However once I add some linq things to it, and specifically the line: WestoriaDataContext westoria = new WestoriaDataContext();
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10097
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.