How to test if variable is assigned?

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

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

What about

int i;


Question by:baltmann

Expert Comment

ID: 9895490
LVL 19

Expert Comment

ID: 9895554
as far as i understood..

int i;
if (i!=null) // if the variable has been assigned

Author Comment

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;
Operator '==' cannot be applied to operands of type 'int' and '<null>'

LVL 20

Accepted Solution

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
  // ok

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

Assisted Solution

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

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

//your int has been set
//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.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is setting this variable making a duplicate in memory 4 30
C# TextBox 11 30
Error on link 14 39
WCF Service Application cannot connect from TCP terminal 1 14
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

12 Experts available now in Live!

Get 1:1 Help Now