Stephen Carter
Stephen Carter

Reputation: 31

C# Variable Declared Outside Of While Loop Is Assigned in Loop But Get 'Unassigned' Compile Error

I am working on an assignment for class and I can't get it to compile. I keep getting "Use of unassigned local variable" for both multiplierString and MultiplcandString. The are declared at the top of the main but it doesn't seem to be assigning a value to them inside the while loops. If i force a value outside the loop, the error goes away. Any help would be most appreciated. Thanks.

What is going on here?

static void Main()
{

    bool goodInput = false;
    ConsoleKeyInfo cki;
    string multiplicandString;
    string multiplierString;
    string endProduct;
    string prompt;
    string response;
    Int64 TryNumber;

    prompt = "This program will multiply two numbers of reasonable length.";
    Console.WriteLine(prompt);
    Console.WriteLine();
    prompt = "Press the Escape (Esc) key to quit or any other key to continue.";
    cki = Console.ReadKey();

    if (cki.Key != ConsoleKey.Escape)
    {
        while (!goodInput)
        {
            prompt = "Please provide the multiplicand: ";
            Console.WriteLine(prompt);
            response = Console.ReadLine();

            if (Int64.TryParse(response, out TryNumber))
            {
                goodInput = true;
                multiplicandString = "a"; //TryNumber.ToString();
            }
            else
            {
                Console.WriteLine();
                prompt = "Invalid multiplicand entry. It must be all numbers.\a";
                Console.WriteLine(prompt);
                prompt = "Please try again.";
                Console.WriteLine(prompt);
            } // end if Int64.TryParse
        } // end while ! goodInput

        goodInput = false;

        while (!goodInput)
        {
            prompt = "Please provide the multiplier: ";
            Console.WriteLine(prompt);
            response = Console.ReadLine();

            if (Int64.TryParse(response, out TryNumber))
            {
                goodInput = true;
                multiplierString = "a"; //TryNumber.ToString();
            }
            else
            {
                Console.WriteLine();
                prompt = "Invalid multiplier entry. It must be all numbers.\a";
                Console.WriteLine(prompt);
                prompt = "Please try again.";
                Console.WriteLine(prompt);
            } // end if Int64.TryParse
        } // end while ! goodInput
          //multiplierString = "a"; //TryNumber.ToString();

        endProduct = MultiplyByRectangle(multiplicandString, multiplierString);

        Console.WriteLine("The result of the calculation is:");
        Console.WriteLine("\t" + endProduct);

    } // end Main()

Upvotes: 3

Views: 4735

Answers (7)

Caleth
Caleth

Reputation: 62939

The problem you have is the compiler does not have a proof that there is a value assigned to those variables. I disagree with the other answers that suggest initialising with null or empty and mutating.

If you pull your two while loops into their own functions, returning string, you can initialise them at the point you declare them.

Upvotes: 0

Mark
Mark

Reputation: 8441

try this:

string multiplicandString = string.Empty;

string multiplierString = string.Empty;

Upvotes: 0

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61972

When TryParse succeeds, the multiplierString is assigned. But when the input is invalid, the else branch runs, and multiplierString is not assigned. Then after the else block multiplierString is read. This is illegal.

Upvotes: -1

V4Vendetta
V4Vendetta

Reputation: 38210

The compiler in this case cannot guess that the variable you declared will be assigned a value.

What if it doesn't enter the loop and even if it does only moves to the else block.

Since these cannot be evaluated a compile time it gives the best feedback as error

So it is better to initialize all your variables like

string multiplicandString ="";

string multiplierString ="";

Upvotes: 0

horgh
horgh

Reputation: 18543

In C# a local variable must be definitely assigned before the first read operation. In your case several variables get initialized only within a loop, while the loop is not absolutely sure to run.

If user presses Esc key at once, these variables will really be unassigned, won't they?

Upvotes: 1

whastupduck
whastupduck

Reputation: 1166

C# needs variables to be definitely assigned. Meaning, variables must been set with an initial value before you it can be read.

Upvotes: 2

Habib
Habib

Reputation: 223272

You are getting the error because compiler can't determine whether you will enter the while loop or not. You can assign them some default value, like `null;

string multiplicandString = null;
string multiplierString = null

Upvotes: 0

Related Questions