我得到一个“错误1”当前上下文中不存在名称“Switch”和“我的交换机语句中的”无效表达式术语'case'“错误。我的问题是这些错误意味着什么?这是开关
private void buttonRead_Click(object sender, EventArgs e)
{
int Selected = listColours.SelectedIndex;
Colours newColour;
Switch(Selected)
{
case 0:
newColour = Colours.red;
MessageBox.Show(newColour.ToString(), "Output", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 1:
newColour = Colours.orange;
MessageBox.Show(newColour.ToString(), "Output", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 2:
newColour = Colours.yellow;
MessageBox.Show(newColour.ToString(), "Output", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 3:
newColour = Colours.green;
MessageBox.Show(newColour.ToString(), "Output", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 4:
newColour = Colours.blue;
MessageBox.Show(newColour.ToString(), "Output", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 5:
newColour = Colours.purple;
MessageBox.Show(newColour.ToString(), "Output", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
答案 0 :(得分:8)
C#
区分大小写,因此您必须完全键入关键字,将Switch
更改为switch
并且它应该有效。