On Sep 4, 3:04 am, "Jure Bogataj" <jure.boga...@mikrocop.comwrote:
Actually, members of the enumeration are declared so only one bit of 32-bit
unsigned int is set.
e.g.
public enum MyEnum1
{
enumValue1 = 0x00000001,
enumValue2 = 0x00000002,
enumValue3 = 0x00000004,
enumValue4 = 0x00000008,
enumValue5 = 0x00000010,
enumValue6 = 0x00000020,
}
Then I check all enumeration / their values the same way. I just have many
enumerations for code clarity?
Best regards,
Jure
First, if you are defining enum values this way, you should attribute
the enum with the [FlagsAttribute]. If not, there is no reason to
define the individual bit values.
What do you mean "check their values" the same way? Enumerations are
meant to be logical groupings. There is no real reason to have two
enumerations have the same semantic meaning (2 exceptions, converting
from an internal interface to a public external interface, or maybe 2
different versions for backward compatibility). It would be
confusing to me as a programmer to run across ten different
enumerations that behaved exactly the same way.
Do you mean that you have a function that does something like "Checks
to see if bit N is set"? (e.g.:
bool IsBitNSet(int bitfield, int bitnum)
{
return (bitfield & (1 << (bitnum - 1))) != 0;
// or
return (bitfield & bitnum) != 0;
}
and then in your code you have:
if (IsBitNSet((int)myEnumVal, 1)) // or IsBitNSet((int)myEnumVal,
0x010111)
{
// Do work.
}
I maintain it is easier to understand the programmer's intent if you
bypass the function call conversions and/or direct bit masking:
MyEnum bitsToCheck = MyEnum.enumValue1 | MyEnum.enumValue4;
if ((myEnumVal & bitsToCheck) != MyEnum.None) // For flags, add a
"None = 0" value.
{
// Do work
}