How get an Array with the EnumValues from a FlagEnum-Value
-
Is it possible to get an Array with the Enum-Values from a FlagEnum-Value ? I mean the following. For example i have a enumation with flag-values like this:
public enum SampleEnum{ Alpha = 1, Beta = 2, Gamma = 4, Delta = 8, Epsilon = 16 }
...and now i got a variable like this:
private SampleEnum var = SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon;
My question is now is there a simple way to bring that values...
SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon
... in an array where the array contains the enum values of the flag-enum-value like this:
private SampleEnum[] varArray = new SampleEnum[3]; SampleEnum[0] SampleEnum.Alpha SampleEnum[1] SampleEnum.Gamma SampleEnum[2] SampleEnum.Epsilon
I tried something like the following but it dont work =/
private SampleEnum var = SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon; private SampleEnum[] varArray = (SampleEnum[]) var ;
I hope you know what i mean.
-
Is it possible to get an Array with the Enum-Values from a FlagEnum-Value ? I mean the following. For example i have a enumation with flag-values like this:
public enum SampleEnum{ Alpha = 1, Beta = 2, Gamma = 4, Delta = 8, Epsilon = 16 }
...and now i got a variable like this:
private SampleEnum var = SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon;
My question is now is there a simple way to bring that values...
SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon
... in an array where the array contains the enum values of the flag-enum-value like this:
private SampleEnum[] varArray = new SampleEnum[3]; SampleEnum[0] SampleEnum.Alpha SampleEnum[1] SampleEnum.Gamma SampleEnum[2] SampleEnum.Epsilon
I tried something like the following but it dont work =/
private SampleEnum var = SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon; private SampleEnum[] varArray = (SampleEnum[]) var ;
I hope you know what i mean.
Hi, have a look at Enum.GetValues() ! PS: It is wise to add the [Flags] attribute if your enum contains only flags :)
Luc Pattyn [My Articles]
-
Is it possible to get an Array with the Enum-Values from a FlagEnum-Value ? I mean the following. For example i have a enumation with flag-values like this:
public enum SampleEnum{ Alpha = 1, Beta = 2, Gamma = 4, Delta = 8, Epsilon = 16 }
...and now i got a variable like this:
private SampleEnum var = SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon;
My question is now is there a simple way to bring that values...
SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon
... in an array where the array contains the enum values of the flag-enum-value like this:
private SampleEnum[] varArray = new SampleEnum[3]; SampleEnum[0] SampleEnum.Alpha SampleEnum[1] SampleEnum.Gamma SampleEnum[2] SampleEnum.Epsilon
I tried something like the following but it dont work =/
private SampleEnum var = SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon; private SampleEnum[] varArray = (SampleEnum[]) var ;
I hope you know what i mean.
You can "cheat" as long as the enum is decorated with the
Flags
attribute by using the following code:SampleEnum var = SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon;
string[] varArray = var.ToString().Split("|");This will give you a string array containing the three enum values as a string. You could then turn each string value back to it's enum value by using
Enum.Parse
.----------------------------- In just two days, tomorrow will be yesterday.
-
Hi, have a look at Enum.GetValues() ! PS: It is wise to add the [Flags] attribute if your enum contains only flags :)
Luc Pattyn [My Articles]
-
You can "cheat" as long as the enum is decorated with the
Flags
attribute by using the following code:SampleEnum var = SampleEnum.Alpha | SampleEnum.Gamma | SampleEnum.Epsilon;
string[] varArray = var.ToString().Split("|");This will give you a string array containing the three enum values as a string. You could then turn each string value back to it's enum value by using
Enum.Parse
.----------------------------- In just two days, tomorrow will be yesterday.
hmm thanks, i thought about that solution already but i hoped that there are an better way to do this..... At least i could write a methode that returns an array with the values like this:
public static Enum[] GetArrayFromFlagEnumValue( Enum flagEnumValue ) { Type flagEnumType = flagEnumValue.GetType(); object[] attributes = flagEnumType.GetCustomAttributes( typeof( FlagsAttribute ), false ); if( attributes.Length > 0 ) { List enumValueArray = new List(); int flagEnumIntValue = Convert.ToInt32( flagEnumValue ); int tmpEnumIntValue = new int(); Array reversedEnumValues = Enum.GetValues( flagEnumType ); Array.Reverse( reversedEnumValues ); foreach( Enum enumValue in reversedEnumValues ) { tmpEnumIntValue = Convert.ToInt32( enumValue ); if( ( flagEnumIntValue - tmpEnumIntValue ) >= 0 ) { flagEnumIntValue = flagEnumIntValue - tmpEnumIntValue; enumValueArray.Add( enumValue ); } } enumValueArray.Reverse(); return enumValueArray.ToArray(); } else { throw new ArgumentException( "The enumation of this value is not marked with the required FlagsAttribute!", "Enum value" ); } }