C# How to check an enum value is passed in a parameter?

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.

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.

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); }.

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 '10 at 15:35 Your code won't compile.

You need to write if((enumParam & MyEnum. A)! = 0) – Jim Mischel Nov 22 '10 at 15:41 @Jim - Of course I do.

Fixed it now. Thanks. – Øyvind Knobloch-Bråthen Nov 22 '10 at 15:43 @Martijn - Is the code I put in my edit what you meant then?

– Øyvind Knobloch-Bråthen Nov 22 '10 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.

– Øyvind Knobloch-Bråthen Nov 22 '10 at 15:56.

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.

That is what I thought he meant also, but apparently it's not ;) – Øyvind Knobloch-Bråthen Nov 22 '10 at 15:53.

As your using flags, this may help you with checking if an enum value has been set: Enum Flags Attribute.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions