Getting values from enum with [Flags] attribute
-
I tried to find it, but Google today doesn't like and doesn't want to tell me this. I have an enum:
[Flags] enum EnumName { None = 0, Value1 = 1, Value2 = 1 << 1, Value3 = 1 << 2, Value4 = 1 << 3, Values13 = Value1 | Value3, Values24 = Value2 | Value4 }
How can I extract from this enum values that are "simple values"... I want to get collection, array, whatever, that will store values:None Value1 Value2 Value3 Value4
I have a couple of enums like this, so "just create a list of those values" isn't good. Also this list is a subject of changes, so I want it to be done automatically after compilation.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
-
I tried to find it, but Google today doesn't like and doesn't want to tell me this. I have an enum:
[Flags] enum EnumName { None = 0, Value1 = 1, Value2 = 1 << 1, Value3 = 1 << 2, Value4 = 1 << 3, Values13 = Value1 | Value3, Values24 = Value2 | Value4 }
How can I extract from this enum values that are "simple values"... I want to get collection, array, whatever, that will store values:None Value1 Value2 Value3 Value4
I have a couple of enums like this, so "just create a list of those values" isn't good. Also this list is a subject of changes, so I want it to be done automatically after compilation.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
You should use the static
Enum.GetValues
method. It retrieves an array of the names of the constants in a specified enumeration. In your case:var names = Enum.GetNames(typeof(EnumName));
/* the produced output will be:
None
Value1
Value2
Value3
Value13
Value4
Value24
*/If you need to get the names as well as their values, use
Enum.GetValues
. I hope this helps.2A
-
You should use the static
Enum.GetValues
method. It retrieves an array of the names of the constants in a specified enumeration. In your case:var names = Enum.GetNames(typeof(EnumName));
/* the produced output will be:
None
Value1
Value2
Value3
Value13
Value4
Value24
*/If you need to get the names as well as their values, use
Enum.GetValues
. I hope this helps.2A
Yeah. I know this. But I want to filter out values that are combination of two or more other values in the enum. So when I have:
Value13 = Value1 | Value3
I want it to be filtered out.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
-
Yeah. I know this. But I want to filter out values that are combination of two or more other values in the enum. So when I have:
Value13 = Value1 | Value3
I want it to be filtered out.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
In this particular case you need only values that evaluate to a power of 2, so you can do something like this:
List<EnumName> filtered = new List<EnumName>(); foreach (EnumName val in Enum.GetValues(typeof(EnumName))) { if ((val & (val - 1)) == 0) filtered.Add(val); }
Now
filtered
will hold allEnumName
values exceptValue13
andValue24
(Which are not powers of 2).2A
-
In this particular case you need only values that evaluate to a power of 2, so you can do something like this:
List<EnumName> filtered = new List<EnumName>(); foreach (EnumName val in Enum.GetValues(typeof(EnumName))) { if ((val & (val - 1)) == 0) filtered.Add(val); }
Now
filtered
will hold allEnumName
values exceptValue13
andValue24
(Which are not powers of 2).2A
Great. Didn't thought of this. Thanks.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
-
I tried to find it, but Google today doesn't like and doesn't want to tell me this. I have an enum:
[Flags] enum EnumName { None = 0, Value1 = 1, Value2 = 1 << 1, Value3 = 1 << 2, Value4 = 1 << 3, Values13 = Value1 | Value3, Values24 = Value2 | Value4 }
How can I extract from this enum values that are "simple values"... I want to get collection, array, whatever, that will store values:None Value1 Value2 Value3 Value4
I have a couple of enums like this, so "just create a list of those values" isn't good. Also this list is a subject of changes, so I want it to be done automatically after compilation.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
[ADDED] Half of this message is wrong, see the replies.[/ADDED] FYI: the presence of
Value13
andValue24
makes your usage of [Flags] inappropriate, see the "guidelines" in MSDN[^]. ]Flags] expects all values to be "bitwise non-overlapping" so if A and B are values, A|B should differ from A as well as from B, which your set does not satisfy. However the compiler does not check this. AFAIK the only implication is on how ToString() will operate. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Friday, May 20, 2011 8:03 AM
-
[ADDED] Half of this message is wrong, see the replies.[/ADDED] FYI: the presence of
Value13
andValue24
makes your usage of [Flags] inappropriate, see the "guidelines" in MSDN[^]. ]Flags] expects all values to be "bitwise non-overlapping" so if A and B are values, A|B should differ from A as well as from B, which your set does not satisfy. However the compiler does not check this. AFAIK the only implication is on how ToString() will operate. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Friday, May 20, 2011 8:03 AM
-
[ADDED] Half of this message is wrong, see the replies.[/ADDED] FYI: the presence of
Value13
andValue24
makes your usage of [Flags] inappropriate, see the "guidelines" in MSDN[^]. ]Flags] expects all values to be "bitwise non-overlapping" so if A and B are values, A|B should differ from A as well as from B, which your set does not satisfy. However the compiler does not check this. AFAIK the only implication is on how ToString() will operate. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Friday, May 20, 2011 8:03 AM
Maybe I'm to tired, but I don't see how does it brakes those guidelines. I have
Value1
,Value2
,Value3
andValue4
as my enumeration values, and for "commonly used flag combinations" I have:Value13
, that meansValue1
ORValue3
andValue24
, that meansValue2
ORValue4
Value13
andValue24
mean "Value 1 or 3", "Value 2 or 4", not "Value 13", "Value 24". Obviously, this enum is just an example and in my real code it's more meaningful.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
-
Maybe I'm to tired, but I don't see how does it brakes those guidelines. I have
Value1
,Value2
,Value3
andValue4
as my enumeration values, and for "commonly used flag combinations" I have:Value13
, that meansValue1
ORValue3
andValue24
, that meansValue2
ORValue4
Value13
andValue24
mean "Value 1 or 3", "Value 2 or 4", not "Value 13", "Value 24". Obviously, this enum is just an example and in my real code it's more meaningful.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
Sorry, you are right, I misunderstood what was written there. Having bit combinations defined in a [Flags] enum is perfectly valid (which explains why I never got a compiler message when I accidentally did what I thought was not allowed). Here is an example of how [Flags] interacts with ToString():
enum Numbers {
Zero=0, One=1, Two=2, Three=3, Four=4, Five=5
}
[Flags]
enum Flags {
Zero=0, One=1, Two=2, Three=3, Four=4, Five=5
}
public override void Test(int arg) {
for (int i=0; i<8; i++) log("Numbers."+i+"="+(Numbers)i);
for (int i=0; i<8; i++) log("Flags."+i+"="+(Flags)i);
}which yields:
13:53:36.826 CPTest.log-59 Numbers.0=Zero
13:53:36.835 CPTest.log-59 Numbers.1=One
13:53:36.846 CPTest.log-59 Numbers.2=Two
13:53:36.862 CPTest.log-59 Numbers.3=Three
13:53:36.874 CPTest.log-59 Numbers.4=Four
13:53:36.884 CPTest.log-59 Numbers.5=Five
13:53:36.896 CPTest.log-59 Numbers.6=6
13:53:36.906 CPTest.log-59 Numbers.7=7
13:53:36.915 CPTest.log-59 Flags.0=Zero
13:53:36.925 CPTest.log-59 Flags.1=One
13:53:36.934 CPTest.log-59 Flags.2=Two
13:53:36.943 CPTest.log-59 Flags.3=Three
13:53:36.951 CPTest.log-59 Flags.4=Four
13:53:36.960 CPTest.log-59 Flags.5=Five
13:53:36.968 CPTest.log-59 Flags.6=Two, Four
13:53:36.977 CPTest.log-59 Flags.7=Two, Five:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.