C# error CS0165: Use of unassigned local variable - ignoring logic and out reference

Access IT picture Access IT · Oct 26, 2011 · Viewed 19.7k times · Source

After searching around I cant seem to locate why the C# compiler is complaining that the local variable dteDest is unassigned in the line

if (dteSrc == dteDest) {

The error goes away if I replace the line

DateTime dteSrc, dteDest;

with

DateTime dteSrc, dteDest = DateTime.MinValue;

As far as I can see the code will never reach the comparison line if dteDest is not initialised by the DateTime.TryParse which it is an out parameter for.

My logic is:

  1. If currentDataObj is null then booHaveOrigDate is false and the first if fails
  2. If currentDataObj is not null but cant be converted to a DateTime then booHaveOrigDate is false and the first if fails
  3. DateTime.TryParse will return false if it cant convert to a DateTime this along with the && means that dteDest will never be used.

Simple Sample Code

void StrangeLogic(object srcData, object currentDataObj) {
   DateTime dteSrc, dteDest;

   bool booHaveNewDate = DateTime.TryParse(srcData.ToString(), out dteSrc);
   bool booHaveOrigDate = (currentDataObj != null) 
                          && DateTime.TryParse(currentDataObj.ToString(), out dteDest);

   if (booHaveNewDate && booHaveOrigDate) {
      if (dteSrc == dteDest) { 
          // Get a "use of unassignned local variable 'dteDest' 
          // unless dteDest = DateTime.MinValue beforehand
      }
   }
}

Also if I change the line

bool booHaveNewDate = DateTime.TryParse(srcData.ToString(), out dteSrc);

to the following

bool booHaveNewDate = (srcData != null) && DateTime.TryParse(srcData.ToString(), out dteSrc);

then the compiler complains that srcDate is not assigned as well.

Could anyone point me in the right direction to what I am missing - I dont mean about parameter checking etc I am concerned with why the compiler logic seems to be fooled by the use of a common TryParse function?

Additional Info

Even expanding out the logic still gives the same error (use of unassigned local variable)

bool booHaveOrigDate;
if (currentDataObj != null) 
   booHaveOrigDate = DateTime.TryParse(currentDataObj.ToString(), out dteDest); 
else 
   booHaveOrigDate = false;

if (booHaveOrigDate) {
    if (dteSrc == dteDest) {

It appears that it is whatever the compiler does with the null checking (currentDataObj != null) that prevents it from correctly determing the dteDest wont be accessed unless assigned

Change it to this code and no problems (aside from the possible .ToString() on a null object

bool booHaveOrigDate = DateTime.TryParse(currentDataObj.ToString(), out dteDest); 
if (booHaveOrigDate) {
    if (dteSrc == dteDest) {

Answer

Peter picture Peter · Oct 26, 2011

Your replace is incorrect, it should be:

DateTime dteSrc = DateTime.MinValue, dteDest = DateTime.MinValue;

However you should use the return variable of TryParse, which is a bool to see if tryparse worked instead if your booHaveNewDate:

DateTime dteSrc, dteDest;

if(DateTime.TryParse(srcData.ToString(), out dteSrc) && DateTime.TryParse(currentDataObj.ToString(), out dteDest))
{
  if (dteSrc == dteDest) { 
      // Do your stuff here
  }
}

Now you do not have to assign the dates in the beginning.

** You should test this code before using, it is no production code and can contain errors