0

real newbie question but why doesn't this work? I am getting

use of unassigned variable 'comparison'

as the error

 string comparison; Console.WriteLine("Enter the first number"); int firstNum = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the second number"); int secondNum = Convert.ToInt32(Console.ReadLine()); if (firstNum == secondNum) comparison = "equals to"; if (firstNum < secondNum) comparison = "less than"; if (firstNum > secondNum) comparison = "greater than"; Console.WriteLine("{0}",comparison); 
1
  • For anyone who has the 2001 Beginning C# by Wrox, you can see this alternative solution on page 66. Commented Dec 15, 2010 at 23:34

5 Answers 5

12

Because the compiler doesn't know comparison was in an executable path. Change the three ifs to if-then-elses:

 if (firstNum == secondNum) comparison = "equals to"; else if (firstNum < secondNum) comparison = "less than"; else comparison = "greater than"; 

and it will work

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

Comments

5

It's almost self explanatory - comparison cannot be guaranteed to be assigned (have a value) and the compiler throws an error as a result of this.

Basically your if statements may never set a value to 'comparison' and this is why it's failing.

A quick and dirty way round this would be to declare comparison in a manner similar to this

string comparison = "unassigned";

or

string comparison = String.Empty;

5 Comments

So in a way, the compiler is pre-empting the problem and kicking up an error in advance? Because the math logic guarantees that comparison will always be assigned a value
In a way - yes. If you are really interested, the links that Daniel has provided will explain it better and in more depth than I can :-]
why is your solution a quick and dirty method? seems like the 'correct' method no?
If Jesse's solution works without warning you can see that it is a more elegant solution as it's slightly clearer what the code is doing and what value will be assigned when you are reading it. Code maintainability is just as important as having something that works.
Actually getting even further in to it - doing it the way I suggested will frequently lead to situations where you assign a value to the string twice (once when it's declared and once when an if statement or whatever matches) - while in your example it won't matter - you could see a performance hit if the code was called often (say in a large loop or something similar)
1

The other answers here are correct. The issue is that C# requires all variables to be definitely assigned before they are used.

Comments

0

You can set comparison to string.Empty as well when you declare it ...

Comments

0

The basic issue here is that the compiler will not check a series of conditionals like this to see that in combination they cover all possible options. Anything in the conditional is considered may-be-executed and that's that, it never looks at the big picture.

As Jesse said, make it a single if else if else so the compiler can see there are no other paths. It's slightly more efficient anyway as your code always executes all three tests whereas his executes at most two tests.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.