switch语句中分配的无法访问的变量

时间:2012-06-09 17:04:54

标签: c# switch-statement

我有以下代码。一个switch语句,它接受一个州名并将另一个变量设置为该状态的abbriviation。但是,在我关闭switch语句后,我收到一条错误消息,指出变量无法访问。

string stateab;

switch (s)
{
    case "Alabama":
        stateab = "AL";
        break;
    case "Alaska":
        stateab = "AK";
        break;
    case "Arizona":
        stateab = "AZ";
        break;
    case "Arkansas":
        stateab = "AR";
        break;
    case "California":
        stateab = "CA";
        break;
    case "Colorado":
        stateab = "CO";
        break;
    case "Connecticut":
        stateab = "CT";
        break;
    case "Delaware":
        stateab = "DE";
        break;
    case "DENIED":
        stateab = "DENIED";
        break;
    case "District of Columbia":
        stateab = "DC";
        break;
    case "Florida":
        stateab = "FL";
        break;
    case "Georgia":
        stateab = "GA";
        break;
    case "Hawaii":
        stateab = "HI";
        break;
    case "Idaho":
        stateab = "ID";
        break;
    case "Illinois":
        stateab = "IL";
        break;
    case "Indiana":
        stateab = "IN";
        break;
    case "Iowa":
        stateab = "IA";
        break;
    case "Kansas":
        stateab = "KS";
        break;
    case "Kentucky":
        stateab = "KY";
        break;
    case "Louisiana":
        stateab = "LA";
        break;
    case "Maine":
        stateab = "ME";
        break;
    case "Maryland":
        stateab = "MD";
        break;
    case "Massachusetts":
        stateab = "MA";
        break;
    case "Michigan":
        stateab = "MI";
        break;
    case "Minnesota":
        stateab = "MN";
        break;
    case "Mississippi":
        stateab = "MS";
        break;
    case "Missouri":
        stateab = "MO";
        break;
    case "Montana":
        stateab = "MT";
        break;
    case "Nebraska":
        stateab = "NE";
        break;
    case "Nevada":
        stateab = "NV";
        break;
    case "New Hampshire":
        stateab = "NH";
        break;
    case "New Jersey":
        stateab = "NJ";
        break;
    case "New York":
        stateab = "NY";
        break;
    case "North Carolina":
        stateab = "NC";
        break;
    case "North Dakota":
        stateab = "ND";
        break;
    case "Ohio":
        stateab = "OH";
        break;
    case "Oklahoma":
        stateab = "OK";
        break;
    case "Oregon":
        stateab = "OR";
        break;
    case "Pennsylvania":
        stateab = "PA";
        break;
    case "Rhode Island":
        stateab = "RI";
        break;
    case "South Carolina":
        stateab = "SC";
        break;
    case "South Dakota":
        stateab = "SD";
        break;
    case "Tennessee":
        stateab = "TN";
        break;
    case "Texas":
        stateab = "TX";
        break;
    case "Utah":
        stateab = "UT";
        break;
    case "Vermont":
        stateab = "VT";
        break;
    case "Virginia":
        stateab = "VA";
        break;
    case "Washington":
        stateab = "WA";
        break;
    case "West Virginia":
        stateab = "WV";
        break;
    case "Wisconsin":
        stateab = "WI";
        break;
    case "Wyoming":
        stateab = "WY";
        break;

}

Console.WriteLine(stateab);

最后一行给出了一个错误,指出该变量在当前上下文中不存在。有什么想法吗?

5 个答案:

答案 0 :(得分:7)

编译器要求您确保在stateab变量可以被读取之前为其赋值。在您的情况下,您需要满足case值不满足s子句的情况(即使您的应用程序的逻辑无法实现)。

您可以通过使用初始值初始化stateab变量来轻松解决这个问题:

string stateab = "";

...或在default声明中加入switch条款:

case "Wyoming":
    stateab = "WY";
    break;
default:
    stateab = "Unknown";
    break;

答案 1 :(得分:2)

您需要break;而不是在每个案例结束时继续......

答案 2 :(得分:0)

使用switch语句时,C#需要默认大小写。没有它它将无法编译。值得尝试一下默认情况。

default:
   //do something
   break;

答案 3 :(得分:0)

这是switch声明的格式:

switch (expression) {    
      case constant-expression: statement
      jump-statement  
      [default:statement
      jump-statement] 
}

您应该包含default个案,并在每个案例结尾处使用break

答案 4 :(得分:0)

您遇到编译器错误,因为如果您将switch语句传递给某个字符串,本地变量可能保持未初始化状态,而这些字符串将不会列在案例中(例如"Hello world")。因此,您应该在第一次使用它之前初始化本地变量。您可以在switch声明之前的声明点执行此操作:

string stateab = "";

或者您可以将default个案添加到您提出异常的switch(或提供缩写的默认值)。

switch (s)
{
    case "Alabama":
         stateab = "AL";
         break;
    // ...
    default:
         throw new ArgumentOutOfRange("s");
}