【问题标题】:Invalid expression term 'case' in switch statmentswitch 语句中的表达式术语“case”无效
【发布时间】:2013-12-03 08:57:12
【问题描述】:

我的 switch 语句中出现“错误 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;
            }

【问题讨论】:

  • 你有没有尝试将Switch关键字变成小写?
  • 应该是switch,而不是Switch(注意大小写)
  • 啊,谢谢你,小语法错误可能会越过你。欢呼

标签: c#


【解决方案1】:

C# 区分大小写,因此您必须准确输入关键字,将 Switch 更改为 switch 应该可以工作。

【讨论】:

    猜你喜欢
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2016-01-18
    • 2013-09-24
    • 2011-06-22
    相关资源
    最近更新 更多