Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Elvenware

Use of Unassigned Local Variable XXX

This error occurs when you try to use a variable that is not initialized to a value. For instance:

string driveName;
Console.WriteLine(driveName);

This will cause an error because driveName has not been initialized to a value. To fix it, you can write this:

string driveName = "Any string";
Console.WriteLine(driveName);

Then the error will go away. You need to look through your code, and see if there is any path through your program that would allow you to arrive at the point where the error occurred without the variable being initialized.

The following code does initialize driveName to the empty string, but there is still a path through the program that would allow driveName to be unitialized at the WriteLine statement:

string driveName;
int x = int.Parse(Console.ReadLine());

if (x < 5)
{                
}
else
{
    driveName = "";
}

Console.WriteLine(driveName);

This would fix the problem:

string driveName;		
int x = int.Parse(Console.ReadLine());

if (x < 5)
{
    driveName = "";
}
else
{
    driveName = "";
}

Console.WriteLine(driveName);