tags:

views:

83

answers:

5

In the following code snippet, if I leave out the line of code that is surrounded by the /////'s, I get an error that reads: "Use of unassigned local variable CurrentDate". It seems a bit silly for me to just give CurrentDate an arbitrary value, is there a better way around this?

DateTime CurrentDate;

///////////////////////////
CurrentDate = DateTime.Now;
///////////////////////////

if(1==1)
{
CurrentDate = DateTime.Now.AddDays(1);
}

if(CurrentDate == DateTime.Now)
{
...
}
+3  A: 

Don't do if (1 == 1)?

Seriously though if the compiler is giving you this error it's usually either because your code is wrong or it's because it's too complex and could be better expressed in another way where you don't need to access possibly unassigned variables.

Can you come up with a real world example where you get this error where there isn't an obvious solution by making a simple refactoring? This would make your question more answerable.

Having said that if you do run into one of these situations there are a few other approaches you could use:

DateTime CurrentDate = DateTime.MaxValue;
DateTime CurrentDate = default(DateTime);
DateTime? CurrentDate = null;

I like the last option because it expresses what you mean - that you don't know the value. It makes the code a little more verbose though as you have an extra level of redirection every time you wish to access a value. You can use the time spent typing .Value to consider whether or not you have correctly handled the situation where it could be null.

Also: Have you considered that the value of DateTime.Now could change between the first and second calls? That final if statement looks like it won't do what you intended.

Mark Byers
@Byers Default value of DateTime is the DateTime.MinValue so instead of using DateTime.MinValue, you assigned DateTime.MaxValue in your example, which is wrong
Adeel
err 'Use of Unassigned Local Variable' needs just that, not complex or horribly wrong code - the message can be recreated in a simple couple lines of code.
John K
@jdk: This compiler error occurs in two situations: 1) your code is wrong. 2) your code is "correct" but the compiler isn't able to prove it (for example here it misses that 1==1 is always true) and so it gives an error. In situation 1) you should fix the code. In situation 2) you either need to a) refactor your code so that you can prove to the compiler that you never access an unassigned variable, or b) workaround the compiler check by assigning some dummy value to the unassigned variable. I interpreted the question as wanting to know the solution for situation 2b.
Mark Byers
@mark: The compiler *does* know that 1 == 1 is always true. The original poster is mistaken; the code given does not produce the stated error. An example of the compiler not knowing that a result is always true would be x * 0 == 0, for integer x. The C# 2.0 compiler evaluated that as always true, but the specification states that *an expression that involves a variable is not to be treated as a constant for the purposes of determining definite assignment*. I fixed that in C# 3.0 to bring it in line with the spec.
Eric Lippert
+1  A: 

You could do this:

DateTime CurrentDate;


if(1==1)
{
CurrentDate = DateTime.Now.AddDays(1);
}
else
{
///////////////////////////
CurrentDate = DateTime.Now;
///////////////////////////
}

if(CurrentDate == DateTime.Now)
{
...
}

This will eliminate the compiler error.

NOTE: in VS2008 this will compile:

if(1==1)
{
CurrentDate = DateTime.Now.AddDays(1);
}
//else
//{
/////////////////////////////
//CurrentDate = DateTime.Now;
/////////////////////////////
//}

if(CurrentDate == DateTime.Now)
{
//
}
code4life
The if (1==1) makes it seem silly to have an else block, but that's the way the C# compiler works, man...!
code4life
+1 for noticing that the code in the question **does** compile, at least in VS 2008. I wonder what compiler he is using then... I've asked a clarifying question.
Mark Byers
A: 

why dont just assigned directly

DateTime CurrentDate =  DateTime.Now; 
Oscar Cabrero
A: 

The example is a bit convoluted, but, no, there isn't a better way around that.

You have to assign a value to a variable heading into a conditional block, even if you are sure that the block will always execute (the 1=1 in your example)

However, I would recommend against DateTime.Now as the initialization value, because it by some mirracle, the block does not execute, the situation should be easily detectable, and DateTime.Now sound way more real than DateTime.MinValue

SWeko
A: 

I always set my objects to null so that it throws a null reference exception if a value is not created. However as Hans pointed out null doesn't work in this situation because in their infinite wisdom Microsoft decided to make DateTime not nullable. As such I normally use DateTime.MinValue since it gives me a specific value to check for and other than time traveling will always be in the past unlike DateTime.MaxValue which is coming up here for some of you archaic 32bit peeps.

Example

DateTime CurrentDate;

///////////////////////////
CurrentDate = DateTime.MinValue;
///////////////////////////

if(1==1)
{
CurrentDate = DateTime.Now.AddDays(1);
}

if(CurrentDate == DateTime.Now)
{
...
}
Jonathan Park
Hint: compile first, post later.
Hans Passant
Good Point Hans, Edited with what I normally do with datetime.
Jonathan Park