C# Enum Flags Comparison

Posted by destructo_gold on Stack Overflow See other posts from Stack Overflow or by destructo_gold
Published on 2010-04-02T14:57:50Z Indexed on 2010/04/02 15:03 UTC
Read the original article Hit count: 292

Filed under:
|

Given the following flags,

  [Flags]
    public enum Operations
    {
        add = 1,
        subtract = 2,
        multiply = 4,
        divide = 8,
        eval = 16,
    }

How could I implement an IF condition to perform each operation? In my attempt, the first condition is true for add, eval, which is correct. However the first condition is also true for subtract, eval, which is incorrect.

        public double Evaluate(double input)
    {
        if ((operation & (Operations.add & Operations.eval)) == (Operations.add & Operations.eval))
            currentResult += input;
        else if ((operation & (Operations.subtract & Operations.eval)) == (Operations.subtract & Operations.eval))
            currentResult -= input;
        else
            currentResult = input;

        operation = null;

        return currentResult;
    }

I cannot see what the problem is.

© Stack Overflow or respective owner

Related posts about enum

Related posts about c#