c# switch problem

主宰稳场 提交于 2019-11-26 17:04:36

问题


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.


回答1:


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:

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 though, which removes your switch statement completely:

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

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.




回答2:


The compiler is complaining because alphaChar is possibly undefined -- if it is not one of the values in your switch then it will not have been defined. You can do one of the following things:

  • Set an initial value of char which will be carried through if none of the switch conditions is true.
  • Add a "default" clause to your switch statement.



回答3:


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.




回答4:


You need to add a default to your switch statement.

The compiler is stating that there are some cases which will not assign a value to the variable. So adding

default:
  alphaChar = 'x'
break;

will tell the compiler "so in case I miss some scenario, make the value this"

or in the case of not wanting to assign a default:

  default: throw new Exception();

This is not necessarily better but another way of doing it:

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



回答5:


You are assigning value to the variable alphaChar based on some condition. Imagine a scenario where the variable x contains value other than 0 to 9. Suppose it contains 10. Then none of the case conditions will be satisfied by x, so alphaChar will not be assigned any value, as a result it will be totally uninitialized. So when you are converting alphaChar 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

case 9: alphaChar = 'J'; 
        break; 

-

default: return null;

and check in the calling methods whether this alphaCoords function returns null or not, like 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
}

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 :).




回答6:


The compiler has no way of knowing that the variable x can only contain numbers up to 9 (which is what you check in your switch). Since the default case is missing from your switch it can happen that alphaChar remains unassigned. You can either add a default case or assign the variable a value before the switch.




回答7:


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




回答8:


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

char alphaChar = 'A';

Or you can do it separately

char alphaChar;    
alphaChar = 'A';


来源:https://stackoverflow.com/questions/1966318/c-sharp-switch-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!