切换语句 - “检测到无法访问的代码”

时间:2015-01-04 15:56:14

标签: string switch-statement

Visual Studio给出了以下错误消息:“检测到无法访问的代码”。谁能告诉我我的代码有什么问题?:

string commandWord = command.CommandWord;   //--> here I get the error message

switch (commandWord) 

(

case "help":

printHelp();

break;



case "go":

goRoom (command);

break; 

case "pick":

pickUp (command);

break; 

)

2 个答案:

答案 0 :(得分:0)

你有一个拼写错误:括号错了,它应该是花括号 - {}

switch (commandWord)

{

case "help":

printHelp();

break;

case "go":

goRoom (command);

break;

case "pick":

pickUp (command);

break;

}

答案 1 :(得分:0)

1)改变 一种可能性:

string commandWord = command.CommandWord;

string commandWord = command.CommandWord();

或者您可能需要包含保护条款:如此:

string commandWord = command.CommandWord;
#pragma warning disable 0162
switch (commandWord)
{
    case "help":
        printHelp();
    break;
    case "go":
        goRoom (command);
    break;
    case "pick":
        pickUp (command);
    break;
}
#pragma warning restore 0162

2)将括号更改为大括号,如下所示:

switch (commandWord)
**{**
    case "help":
        printHelp();
    break;
    case "go":
        goRoom (command);
    break;
    case "pick":
        pickUp (command);
    break;
**}**
在MAC上,你有大括号:

  • 对于{使用以下组合:
  

Alt +(

  • For}使用以下组合:
  

Alt +)