Enum flags

  • Thread starter Thread starter Matt Burland
  • Start date Start date
M

Matt Burland

I wanted to set up a enum to represent certain properties of an object that
can be combined. So I'm using the FlagAttribute with my enum and adding them
to by objects with | (or). But what is the right way to get them back (i.e.
to check it this object has a certain flag set)? I tried:

(MyObj.Properties & MyProps.Prop1) == MyProps.Prop1

But that doesn't work right. What gives?

Thanks

Matt
 
Matt,
(MyObj.Properties & MyProps.Prop1) == MyProps.Prop1

But that doesn't work right. What gives?

In what way does it not work? How did you define your enum? What's the
value of Prop1?



Mattias
 
Hi Matt
(MyObj.Properties & MyProps.Prop1) == MyProps.Prop1
This is the right way to chack the flags.
The problem I guess is in MyProps enum declaration.
You have to defined like this

[Flags]
enum MyProps
{
Prop1 = 1,
Prop2 = 2,
Prop3 = 4,
Prop4 = 8
//and so on
}

Adding FalgsAttribute attribute doesn't set your enum values to be powers
of 2. If you have overlapping flags that checking may not work.

HTH
B\rgds
100
 
Yeap, that was it. It just dawned on me a minute ago. Thanks for the
response.

100 said:
Hi Matt
(MyObj.Properties & MyProps.Prop1) == MyProps.Prop1
This is the right way to chack the flags.
The problem I guess is in MyProps enum declaration.
You have to defined like this

[Flags]
enum MyProps
{
Prop1 = 1,
Prop2 = 2,
Prop3 = 4,
Prop4 = 8
//and so on
}

Adding FalgsAttribute attribute doesn't set your enum values to be powers
of 2. If you have overlapping flags that checking may not work.

HTH
B\rgds
100
 
Back
Top