Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to test if variable is assigned?

Posted on 2003-12-08
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 9895490
LVL 19

Expert Comment

by:Fahad Mukhtar
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 200 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

609 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