C # switch problem

I am new to programming and have 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: using the unassigned local variable 'alphaChar'

But I assign it in my switch block.

I am sure this is my mistake as I do not know enough about programming.

Please inform.

Thanks.

+7
c # switch-statement
source share
8 answers

You assign it if x is 0-9. What would you expect from this if x were 123? Although you may know that only values ​​between 0 and 9 will be passed, the compiler does not do this - so it needs to consider what will happen differently.

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 is not in the expected range:

 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; default: throw new ArgumentOutOfRangeException(); } 

Here's a slightly simpler alternative that completely removes the switch statement:

 if (x < 0 || x > 9) { throw new ArgumentOutOfRangeException(); } char alphaChar = (char)('A' + x); 

Please note that when using arithmetic you need to be careful. In Java and C #, the basic representation is guaranteed to be Unicode, which makes life a lot easier. I believe this is good for such things (and hexagoring / formatting), but when you go into more exotic scenarios, it fails. Again, this is true for many code simplification methods ... if they are applied inappropriately, you get a mess.

+34
source share

You need to add the default value to the switch statement.

The compiler states that there are some cases that will not assign a value to a variable. Therefore adding

 default: alphaChar = 'x' break; 

will tell the compiler "so if I skip some script, make that value"

or in case you do not want to assign a default value:

  default: throw new Exception(); 

This is not necessarily better, but another way to do it:

  private string alphaCoords(Int32 x) { if(x >= 0 && x =< 9) return ((char)(x + 65)).ToString(); else throw new ArgumentException(); } 
+2
source share

The compiler complains because alphaChar is possibly undefined - if it is not one of the values ​​in switch , then it will not be defined. You can do one of the following:

  • Set the initial char value that will be migrated if none of the switching conditions are true.
  • Add a default clause to the switch statement.
+2
source share

Before its first use, a local variable must be definitely assigned (in accordance with the rules of the C # specification). In this particular case, the switch construct does not guarantee that alphaChar will be definitely assigned, thus a compiler error. You can provide an initial alphaChar value and therefore it will definitely be assigned.

+2
source share

You assign a value to the alphaChar variable based on some condition. Imagine a scenario in which the variable x contains a value other than 0 to 9. Assume that it contains 10. Then none of the conditions of the condition will be satisfied by x , so alphaChar will not be assigned any value, as a result it will be completely uninitialized . Therefore, when you convert alphaChar to a string, it converts some garbage value to a string and returns it to the calling method. That is why you get this message.

If you want a simple solution, add the following code below

 case 9: alphaChar = 'J'; break; 

-

 default: return null; 

and check the calling methods whether this alphaCoords function alphaCoords null or not, for example this -

 if(alphaCooord(10) == null) { // x contains value other than 0 to 9 } else { // x contains value between 0 to 9, so the returned value will be the string // representation of the corresponding character } 

That way, your code will not be too complicated, or you will not need to throw or handle any exceptions or anything like that.

Hope that helps :).

+1
source share

The compiler does not know that the variable x can only contain numbers up to 9 (this is what you check in switch ). Since there is no default case on your switch, it may happen that alphaChar remains unassigned. You can either add a default case, or assign a variable a value before switch .

0
source share

Add a default value to your switch, because if x is 10, alphaChar will never be assigned.

0
source share

After declaring a char alphaChar variable, you should β€œassign” a value to it (set it equal to something), even if you expect to get a value in the switch statement.

You can assign it to "A" or "0" or just about anything.

You can do it in an ad like this

 char alphaChar = 'A'; 

Or you can do it separately

 char alphaChar; alphaChar = 'A'; 
0
source share

All Articles