0

I have a method which accepts an enum as an argument:

[Flags]
public enum MyEnum
{
  A = 1,
  B = 2,
  C = 3
}

public class MyClass
{
  public MyEnum myEnum;
}

public bool MyMethod(MyClass class, MyEnum enumParam)
{
  // Here I want to check if object class contains some enum values passed as an argument
  // Something like: if(class.myEnum 'contains-one-of-the-items-of enumParam)
}

public void Test()
{
  Myclass class = new MyClass() { myEnum = MyEnum.A };
  MyMethod(class, MyEnum.A | MyEnum.B);
}

I want to check if an object contains one of the enum values which are passed in the method as an argument.

5 Answers 5

3

As your using flags, this may help you with checking if an enum value has been set: What does the [Flags] Enum Attribute mean in C#?

2

You can write it like this

public bool MyMethod(MyClass class, MyEnum enumParam)
{
  if( (enumParam & MyEnum.A) != 0 ){
    ...
  }
  if( (enumParam & MyEnum.B) != 0 ){
    ...
  }
}

I changed enum to enumParam to not conflict with the enum keyword.

There is also a problem with your implementation since you have the values 1,2,3 for A,B,C. This way you can't differentiate between A+B=3 and C=3. A should be 1, B should be 2 and C should be 4 (D should be 8 and so on)

EDIT

Edit due to OP's comment.

public bool MyMethod(MyClass class, MyEnum enumParam)
{
  return Enum.IsDefined(typeof(MyEnum), enumParam);
}
5
  • I'm sorry, but this is not what I meant. I want to check if a value of enumParam exists in the class.myEnum
    – Martijn
    Nov 22, 2010 at 15:35
  • Your code won't compile. You need to write if((enumParam & MyEnum.A) != 0) Nov 22, 2010 at 15:41
  • @Martijn - Is the code I put in my edit what you meant then? Nov 22, 2010 at 15:44
  • @Martijn - When looking closer there are still two strange things in your code. 1. You have two arguments to MyMethod, but it seems you only actually need to use one. When calling the method from Test() you only provide one parameter. So it is one or two? My answer assumes 1, and don't care about the class parameter (that is also poorly names since class is a reserved keyword as well. Nov 22, 2010 at 15:56
  • @Oyvind: You're right, I have edited my post. I am not familiar with the IsDefined method, I will look it up, but I think it is indeed what I need. Thanx for the effort.
    – Martijn
    Nov 22, 2010 at 16:07
1

If you want to see if any of the values passed in the parameter are in the class's myEnum field, you can write:

public bool MyMethod(MyClass class, MyEnum enum)
{
  // Here I want to check if object class contains some enum values passed as an argument
  // Something like: if(class.myEnum 'contains-one-of-the-items-of enum)
  return (this.myEnum & enum) != 0;
}

This does a logical "AND" of the bit flags and will return true if any one of the flags in enum is set in myEnum.

If you want to ensure that all the flags are set, then you can write:

return (this.myEnum & enum) == this.myEnum;

Also, read the response by @Øyvind Bråthen carefully. In order for [Flags] to work, you need to ensure that your enum values are powers of 2.

1

Change your enum like this :

public enum MyEnum
    {
        A = 2,
        B = 4,
        C = 8
    }

and your method is as simple as :

public bool MyMethod(MyClass aClass, MyEnum aEnum)
        {
            return (aClass.myEnum & aEnum) != 0;
        }

Best regards

1
  • That is what I thought he meant also, but apparently it's not ;) Nov 22, 2010 at 15:53
0

In C# 4.0 you can easily use Enum.HasFlag method. You can take a look at this question to get other solutions including C# 3.5 and previous versions.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.