简体   繁体   中英

C# How to Make Enum as a switch?

how use enum values in switch cases ? 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.

Your current example method has value hard coded to "Mult" but I assume that would be changed in your actual implementation.

Assuming "value" contains the enumeration value corresponding to the button clicked, your Switch Statement should switch on that variable:

switch(value)

Inbetween the parentheses you don't use "operations" (which is a type) but a variable (which you named "value" 5 lines before). 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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