473,382 Members | 1,720 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,382 software developers and data experts.

check if object is null

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

Nov 22 '05 #1
2 2722
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 22 '05 #2
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 <gi****@cantoberon.it>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)X-Accept-Language: en-us, en
MIME-Version: 1.0
Subject: check if object is null
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <uL**************@TK2MSFTNGP11.phx.gbl>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 217.220.125.116
Lines: 1
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:115445
X-Tomcat-NG: microsoft.public.dotnet.general

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


Nov 22 '05 #3

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

Similar topics

5
by: LL | last post by:
Hi, Where is the best place to check the session's object, if it's null, then redirect the user to login page? put into every page's load event? Thanks.
2
by: aaa | last post by:
I know in VB we had IsObject ut how do I check an object to see if it exists in sharp (without bombing the if)?
9
by: Edmund Dengler | last post by:
Greetings! Just trying some tests out, and wanted to know about some optimizations. If I do a CHECK constraint on a table, is this used to optimize a SELECT or does Postgresql rely mostly on...
25
by: pamelafluente | last post by:
Hi Guys, I have the following HTML code which is doing a GET to a page, say MyUrl.aspx : <body> <form name="form1" method="get" action="MyUrl.aspx" id="form1"> <input type="hidden"...
4
by: Karen Hill | last post by:
How does one check if an object's reference is null? I'd like to check that rather than catch a nullreference exception. thanks.
15
by: Tarun Mistry | last post by:
Hi guys, what is the best/correct way to check for a NULL object? I.e. myClass test; if(test == null) {}
8
by: Earl | last post by:
What's the best way to check for null on an ExecuteScalar? The following would fire the command twice: if (cmd.ExecuteScalar() != null) { intContactID = Convert.ToInt32(cmd.ExecuteScalar()); }
30
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
14
by: Mark | last post by:
Hi, I would like to check if my object has been deleted or not, but my program keeps crashing. Here's the simplified code (in infinite loop) delete tetromino; //if(tetromino==NULL)...
4
by: rosh72851 | last post by:
Header file #ifndef STRING_HPP #define STRING_HPP #include <iostream> using namespace std; /* mystring class */
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.