Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How get an Array with the EnumValues from a FlagEnum-Value

How get an Array with the EnumValues from a FlagEnum-Value

Scheduled Pinned Locked Moved C#
questiondata-structuresbeta-testingtutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarkPhB
    wrote on last edited by
    #1

    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.

    L S 2 Replies Last reply
    0
    • M MarkPhB

      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.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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]

      M 1 Reply Last reply
      0
      • M MarkPhB

        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.

        S Offline
        S Offline
        Scott Dorman
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • L Luc Pattyn

          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]

          M Offline
          M Offline
          MarkPhB
          wrote on last edited by
          #4

          No, dont want all values of the enum .....i just want the enum-values of the variable. Scott Dorman understood it right ;)

          1 Reply Last reply
          0
          • S Scott Dorman

            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.

            M Offline
            M Offline
            MarkPhB
            wrote on last edited by
            #5

            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" );
            	}
            }
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups