check if object is null

  • Thread starter Thread starter Giulio Mastrosanti
  • Start date Start date
G

Giulio Mastrosanti

How can I check if a declared object in not initialized yet with a new
command?
is catching the exception while accessing it the only way?

thanx
 
Giulio,
How can I check if a declared object in not initialized yet with a new
command?

if ( obj == null ) ...

is catching the exception while accessing it the only way?

No, it's probably the worst way.



Mattias
 
Guilio

You can also check by using the == operator.

For example:

using System;

class nullcheck
{
private static Foo f;

[STAThread]
static void Main(string[] args)
{
// f = new Foo();
if (f == null)
Console.WriteLine("Foo is not initialized!!!");
else
Console.WriteLine("Foo is initialized.");
return;
}
}

class Foo
{
public int x = -1;
public Foo()
{
x = 5;
}
}

Cheers,
Christian T. [MSFT]
Visual Studio Update Team

- Please do not reply to this email directly. This email is for newsgroup
purposes only.
=========================================================================
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which
they originated.
=========================================================================

--------------------
Date: Wed, 12 Nov 2003 22:28:51 +0100
From: Giulio Mastrosanti <[email protected]>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4)
Gecko/20030624 Netscape/7.1 (ax)
 

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

Back
Top