3

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()
3
  • try to assign as "" or string.empty Commented Feb 7, 2013 at 6:19
  • Besides your code is missing a closing bracket for if (cki.Key != ConsoleKey.Escape) {... Commented Feb 7, 2013 at 6:23
  • 1
    Think about what happens if the very first key that the user presses is Esc. Commented Feb 7, 2013 at 7:08

7 Answers 7

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

0

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

3 Comments

But this is not useful unless you are willing to use the null values. Instead, change the program flow so that the variable is never read before it is assigned to (with a valid value).
@JeppeStigNielsen, yes that would be ideal, but sometimes its not just possible, for example if you are setting up something in the if block and then returning it outside of your if block. Assigning something in the try block and then using it outside would be a better example
Agreed there could be complex cases. But in a try-catch scenario I would prefer assigning the outer variable both in the try block and in the catch block(s). Or making the end of the catch block unreacable by returning or re-throwing from it. If you choose to initialize with null, the compiler won't help you figure out if a "good" value was assigned, and you will get a problem runtime instead! Of course if null is considered a "good value" in the situation, I have no objections at all.
0

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 ="";

Comments

0

try this:

string multiplicandString = string.Empty;

string multiplierString = string.Empty;

1 Comment

Care to comment/explain what is wrong in my answer, Mr Downvoter?
0

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.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.