ohyeah - 8 months ago 35

C# Question

I can't figure out how to use switches in combination with an enum. Could you please tell me what I'm doing wrong, and how to fix it? I have to use an enum to make a basic calculator.

`public enum Operator`

{

PLUS, MINUS, MULTIPLY, DIVIDE

}

public double Calculate(int left, int right, Operator op)

{

int i = (int) op;

switch(i)

{

case 0:

{

return left + right;

}

case 1:

{

return left - right;

}

case 2:

{

return left * right;

}

case 3:

{

return left / right;

}

default:

{

return 0.0;

}

}

}

The end result should be something like this:

`Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, PLUS))`

Output: The sum of 5 and 5 is 10

Could you guys please tell me how I'm messing up?

Answer

You don't need to convert it

```
switch(op)
{
case Operator.PLUS:
{
}
}
```

By the way, use brackets

Source (Stackoverflow)