I'm new to programming and having a problem with the following code:
private string alphaCoords(Int32 x)
{
char alphaChar;
switch (x)
{
case 0: alphaChar = 'A'; break;
case 1: alphaChar = 'B'; break;
case 2: alphaChar = 'C'; break;
case 3: alphaChar = 'D'; break;
case 4: alphaChar = 'E'; break;
case 5: alphaChar = 'F'; break;
case 6: alphaChar = 'G'; break;
case 7: alphaChar = 'H'; break;
case 8: alphaChar = 'I'; break;
case 9: alphaChar = 'J'; break;
}
return alphaChar.ToString();
}
The compiler says: Use of unassigned local variable 'alphaChar'
But I'm assigning it in my switch block.
I'm sure this is my fault as I dont know enough about programming.
Please advise.
Thanks.
You are assigning value to the variable
alphaChar
based on some condition. Imagine a scenario where the variablex
contains value other than 0 to 9. Suppose it contains 10. Then none of the case conditions will be satisfied byx
, soalphaChar
will not be assigned any value, as a result it will be totally uninitialized. So when you are convertingalphaChar
to string, it is converting some garbage value to string and returning it to the calling method. This is the reason why you are getting that message.If you want to get a simple solution, then add the following code below
-
and check in the calling methods whether this
alphaCoords
function returns null or not, like this -In this way your code won't be too complex, or you won't need to throw or handle any exceptions or something like that.
Hope that helps :).
You're assigning it if x is 0-9. What would you expect it to do if
x
were 123 though? While you may know that only values between 0 and 9 will be passed in, the compiler doesn't - so it needs to consider what would happen otherwise.One way to avoid this is to have a
default
case in your switch statement, which you can use to throw an exception if the value isn't in the expected range:Here's a slightly simpler alternative though, which removes your switch statement completely:
Note that you do need to exercise care when using arithmetic like this. In Java and C# the underlying representation is guaranteed to be Unicode, which makes life a lot easier. I believe it's fine for things like this (and hex parsing/formatting) but when you venture into more exotic scenarios it would fail. Then again, that's true for a lot of code simplification techniques... if they're applied inappropriately, you end up with a mess.
The compiler has no way of knowing that the variable
x
can only contain numbers up to 9 (which is what you check in yourswitch
). Since thedefault
case is missing from your switch it can happen thatalphaChar
remains unassigned. You can either add adefault
case or assign the variable a value before theswitch
.Before its first use local variable must be definitely assigned (according to C# specification rules). In this particular case switch construct doesn't guarantee that alphaChar will be definitely assigned thus compiler error. You can provide initial value to alphaChar and thus it will be definitely assigned.
Add a default to your switch, because if x is 10, alphaChar will never be assigned.
After you declare the variable char alphaChar, you should "assign" a value to it (set it to be equal to something), even though you expect it to get a value in the switch statement.
You could assign it 'A' or '0' or pretty much anything.
You can do that in the declaration like this
Or you can do it separately