c# switch problem

2019-01-12 03:39发布

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.

8条回答
Fickle 薄情
2楼-- · 2019-01-12 04:11

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

查看更多
成全新的幸福
3楼-- · 2019-01-12 04:22

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.

查看更多
Fickle 薄情
4楼-- · 2019-01-12 04:25

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.

查看更多
神经病院院长
5楼-- · 2019-01-12 04:27

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.

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-12 04:29

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

查看更多
女痞
7楼-- · 2019-01-12 04:31

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';
查看更多
登录 后发表回答