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
No need to convert. You can apply conditions on Enums inside a switch. Like so,
public enum Operator
{
PLUS,
MINUS,
MULTIPLY,
DIVIDE
}
public double Calculate(int left, int right, Operator op)
{
switch (op)
{
case Operator.PLUS: return left + right;
case Operator.MINUS: return left - right;
case Operator.MULTIPLY: return left * right;
case Operator.DIVIDE: return left / right;
default: return 0.0;
}
}
Then, call it like this:
Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, Operator.PLUS));