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. Getting values from enum with [Flags] attribute

Getting values from enum with [Flags] attribute

Scheduled Pinned Locked Moved C#
data-structuresquestion
9 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.
  • L Offline
    L Offline
    Lukasz Nowakowski
    wrote on last edited by
    #1

    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.

    M L 2 Replies Last reply
    0
    • L Lukasz Nowakowski

      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.

      M Offline
      M Offline
      markovl
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • M markovl

        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

        L Offline
        L Offline
        Lukasz Nowakowski
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • L Lukasz Nowakowski

          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.

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

          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 all EnumName values except Value13 and Value24 (Which are not powers of 2).

          2A

          L 1 Reply Last reply
          0
          • M markovl

            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 all EnumName values except Value13 and Value24 (Which are not powers of 2).

            2A

            L Offline
            L Offline
            Lukasz Nowakowski
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • L Lukasz Nowakowski

              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.

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

              [ADDED] Half of this message is wrong, see the replies.[/ADDED] FYI: the presence of Value13 and Value24 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

              L M 2 Replies Last reply
              0
              • L Luc Pattyn

                [ADDED] Half of this message is wrong, see the replies.[/ADDED] FYI: the presence of Value13 and Value24 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

                M Offline
                M Offline
                markovl
                wrote on last edited by
                #7

                Good call :)

                2A

                1 Reply Last reply
                0
                • L Luc Pattyn

                  [ADDED] Half of this message is wrong, see the replies.[/ADDED] FYI: the presence of Value13 and Value24 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

                  L Offline
                  L Offline
                  Lukasz Nowakowski
                  wrote on last edited by
                  #8

                  Maybe I'm to tired, but I don't see how does it brakes those guidelines. I have Value1, Value2, Value3 and Value4 as my enumeration values, and for "commonly used flag combinations" I have: Value13, that means Value1 OR Value3 and Value24, that means Value2 OR Value4 Value13 and Value24 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.

                  L 1 Reply Last reply
                  0
                  • L Lukasz Nowakowski

                    Maybe I'm to tired, but I don't see how does it brakes those guidelines. I have Value1, Value2, Value3 and Value4 as my enumeration values, and for "commonly used flag combinations" I have: Value13, that means Value1 OR Value3 and Value24, that means Value2 OR Value4 Value13 and Value24 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.

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

                    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.

                    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