Solved

How to test if variable is assigned?

Posted on 2003-12-08
5
413 Views
Last Modified: 2008-03-06
System.DateTime d;

How do you test if the variable d has been assigned?

What about

int i;

?

0
Comment
Question by:baltmann
5 Comments
 
LVL 3

Expert Comment

by:monkesdb
ID: 9895490
eh?
0
 
LVL 19

Expert Comment

by:Desp
ID: 9895554
as far as i understood..

int i;
....
if (i!=null) // if the variable has been assigned
{
...
}
0
 

Author Comment

by:baltmann
ID: 9895811
The compile errors are:

with DateTime:

System.DateTime dt ;
if(dt != null)
...
Operator '!=' cannot be applied to operands of type 'System.DateTime' and '<null>'

with int:
int i;
if(i==null){i=0;}
...
Operator '==' cannot be applied to operands of type 'int' and '<null>'

0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 50 total points
ID: 9896285
An object is checked with o != null. Objects are strings, forms, arraylists, etc. However DateTime and int are value types, which means they cannot be compared to null.

So you have to implement your own logic. For example if you need only positive numbers you can do like this:

int i = -1;
...
if (i < 0)
  // still no real value
else
  // ok

For DateTime you can make:
DateTime dt = DateTime.MinValue;
...
if (dt == DateTime.MinValue)
  // still no real value
else
  // ok
0
 
LVL 7

Assisted Solution

by:jj819430
jj819430 earned 50 total points
ID: 9897533
int i;
bool iSet = false;

// whenever you set i, set iSet = true;

if(iSet)
{
//your int has been set
}
if(!iSet)
{
//your int has not been set
}

All the others were just as valid a way of doing this, because they do not require the added overhead. I  like this approach only because it doesn't limit the functionality of the int. If you will only be using positive or only be using negative numbers I would suggest you use "TheAvenger"'s approach for the speed. If you need both Positive and Negative integer values, then this may be your best approach.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now