C # variable declared outside of loop is assigned in Loop but gets "Unassigned" Compile Error

I am working on assignment for a class and I am unable to compile it. I keep getting "Use of unassigned local variable"

for multiplierString

and MultiplcandString

. They're declared at the top of the main one, but don't seem to assign values ​​to them inside while loops. If I force the value outside of the loop, the error goes away. Any help would be greatly appreciated. Thank.

What's 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()

      

+3


source to share


7 replies


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



+2


source


In C #, a local variable must be assigned before the first read operation. In your case, multiple variables are initialized only inside the loop, while the loop doesn't necessarily work.



If the user presses a key Esc

right away, these variables won't really be assigned, right?

+1


source


You are getting the error because the compiler cannot tell if you will enter the while loop or not. You can give them some default value, such as `null;

string multiplicandString = null;
string multiplierString = null

      

0


source


In this case, the compiler cannot guess that the declared variable will be assigned a value.

What if it does not enter the loop and even if it only moves to the else block.

Since they cannot be estimated by compile time, it gives better feedback as a bug

So it is better to initialize all your variables like

string multiplicandString ="";

string multiplierString ="";

      

0


source


try this:

string multiplicandString = string.Empty;

string multiplierString = string.Empty;

      

0


source


You have a compiler problem, you have no proof that these variables have a value. I disagree with other answers that assume null or empty initialization and mutation.

If you pull your two while loops into your own functions by returning a string, you can initialize them where you declare them.

0


source


When he TryParse

succeeds, he is appointed multiplierString

. But when the input is invalid, the branch is else

started rather multiplierString

than assigned. Then after reading the block else

multiplierString

. It's illegal.

-1


source







All Articles