简体   繁体   English

C# 如何使枚举作为开关?

[英]C# How to Make Enum as a switch?

how use enum values in switch cases ?如何在 switch case 中使用枚举值? here is my code :这是我的代码:

    //public Variables        
    double rat;
    string M;
    public enum operations {add = 1, sub = 2, mult = 3, div = 4} ;
    bool NewText = false;

    private void btnmult_Click(object sender, EventArgs e)
    {
        //Button "*"  
        operations value = operations.mult;
        rat = Convert.ToDouble(result.Text);
        label1.Text = rat + " *";
        result.Text = "";
    }
      private void EqualButton_Click(object sender, EventArgs e)
    {
        switch (operations) // this is the point which is am confused at
        {            
            case (operations.mult): //multiplyication
                TheEqualMult(rat);
                label1.Text = "";
                break;
        }
    }

now What i should write after the word switch between the () ?现在我应该在 () 之间切换单词后写什么? what i want to type is the enum value which refers to 1 or 2 or 3 or 4 so the button can know which is right operation to go in.我想输入的是枚举值,它指的是 1 或 2 或 3 或 4,因此按钮可以知道哪个是正确的操作。

Your current example method has value hard coded to "Mult" but I assume that would be changed in your actual implementation.您当前的示例方法具有硬编码为“Mult”的值,但我认为在您的实际实现中会发生变化。

Assuming "value" contains the enumeration value corresponding to the button clicked, your Switch Statement should switch on that variable:假设“value”包含与单击的按钮对应的枚举值,您的 Switch 语句应该打开该变量:

switch(value)

Inbetween the parentheses you don't use "operations" (which is a type) but a variable (which you named "value" 5 lines before).在括号之间,您不使用“操作”(这是一种类型)而是一个变量(您在 5 行之前将其命名为“值”)。 Also in the cases (two lines after the troublesome line) you don't need to use parentheses (they don't do harm except for others you collaborate with)同样在情况下(麻烦行之后的两行)您不需要使用括号(除了与您合作的其他人之外,它们不会造成伤害)

The following is what you are looking for.以下是您正在寻找的内容。

Note that when declaring enums, values will be automatically assigned incrementally.请注意,在声明枚举时,值将自动递增分配。 By default the first value is zero, unless you override it.默认情况下,第一个值为零,除非您覆盖它。

In your switch statement you use the variable whose value you are switching on - in your case value.在您的 switch 语句中,您使用要打开其值的变量 - 在您的 case 值中。

double rat;
string M;
public enum operations 
{
    add = 1,
    sub,
    mult,
    div
} ;
bool NewText = false;
operations value;

private void btnmult_Click(object sender, EventArgs e)
{
    //Button "*"  
    value = operations.mult;
    rat = Convert.ToDouble(result.Text);
    label1.Text = rat + " *";
    result.Text = "";
    }

private void EqualButton_Click(object sender, EventArgs e)
{

    switch (value) // use a variable at this point
    {            
        case operations.mult: //multiplication
            TheEqualMult(rat);
            label1.Text = "";
            break;
        case operations.add: //addition
            ....
            break;
        case operations.sub: //subtraction
            ....
            break;
        case operations.div: //division
            ....
            break;
        default: //invalid operation
            ....
            break;
    }
}

Note that in the above code value will default to zero - effectively an invalid enum - so you need the default at the end.请注意,在上面的代码中,值将默认为零——实际上是一个无效的枚举——所以你需要在最后使用默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM