7

The common way of checking equality on multiple variables within an if condition is as following.

public enum Values
{
    Value1,
    Value2,
    Value3
}
void MethodName (Values randomValue )
{
 if (randomValue == Values.Value1|| randomValue == Values.Value2)
  {
       // code here
  }
}

Rather than having an OR condition, is there a better way of doing this ?

3
  • better option will be a switch Commented May 31, 2016 at 3:18
  • "common way of checking equality on multiple variables" is actually different - stackoverflow.com/questions/3907299/…... But you already should have found it before and disliked. For future questions please clarify your preference (otherwise it look like you have not done any research and ...) Commented May 31, 2016 at 3:34
  • Enums better to work in switch statements rather than if blocks. If you want to have multiple equality conditions, define logic for a main case and use goto case which point to main case for another related cases. Commented May 31, 2016 at 3:42

3 Answers 3

11

A few options:

  1. You can define your enums as flags. This means each value must be a power of 2 (1, 2, 4, 8, etc). Then, you could write:

if (randomValue & (Values.Value1 | Values.Value2) > 0)
{
     //...
}
  1. You can use a switch

switch (randomValue)
{
    case Values.Value1:
    case Values.Value2:
    {
        //Do something
        break;
    }
    case Values.Value3:
        //Do something else
        break;
    default:
        break;
}
  1. You can use an array (nicer if you have predefined sets of values you want to search for).

if (new[] { Values.Value1, Values.Value2 }.Contains(randomValue))
{
}

or

static(?) readonly Values[] allowedValues = new[] { Values.Value1, Values.Value2 };

void MethodName(Values randomValue)
{
    if (allowedValues.Contains(randomValue))
    {
        //...
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

how about the other way around, sounds like the question could be "How to check if a value of a variable is defined as an enum value?" and then do if (Enum.IsDefined(typeOf(YourEnum, YourValue) { }? works for me :)
I do like the bitwise discussion in the flags post, thanks for posting!
@JanneAndersson That won't work for their given example, though, which is checking if it's Value1, Value2, but not Value3
0

By using Enum Flags we can do comparisons as follows:

[Flags]
public enum Values
{
  Value1 = 1 << 0,
  Value2 = 1 << 1,
  Value3 = 1 << 2
}

void MethodName(Values randomValue)
{
  if ((randomValue & (Values.Value1 | Values.Value2)) != 0)
  {
    // code here
  }
}

Note the Flags attribute and the bit shifting required for the Enum values. The Enum values can also be defined as:

  [Flags]
  public enum Values
  {
    Value1 = 1,
    Value2 = 2,
    Value3 = 4
  }

We can now parameterize the Allowed Values as follows:

      [Flags]
      public enum Values
      {
        Value1 = 1 << 0,
        Value2 = 1 << 1,
        Value3 = 1 << 2
      }    

      void MethodName(Values randomValue)
      {
        if (IsValid(randomValue, Values.Value1 | Values.Value2))
        {
          // code here
        }
      }

      bool IsValid(Values randomValue, Values allowedValues)
      {
        return ((randomValue & (allowedValues)) != 0);
      }

Comments

-2

See this example

public enum Level {

HIGH,
MEDIUM,
LOW

}

You can refer to the constants in the enum above like this:

Level level = Level.HIGH;

Level level = ... //assign some Level constant to it

if( level == Level.HIGH) {

} else if( level == Level.MEDIUM) {

} else if( level == Level.LOW) {

}

1 Comment

Not clear how this code simplifies multiple conditions check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.