c# switch problem

后端 未结 8 2357
囚心锁ツ
囚心锁ツ 2020-12-03 18:53

I\'m new to programming and having a problem with the following code:

    private string alphaCoords(Int32 x)
    {
        char alphaChar;

        switch (         


        
8条回答
  •  感情败类
    2020-12-03 19:09

    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();
        }
    

提交回复
热议问题