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. Flags Enums

Flags Enums

Scheduled Pinned Locked Moved C#
questionregex
6 Posts 3 Posters 1 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.
  • U Offline
    U Offline
    User 260964
    wrote on last edited by
    #1

    This is propably a question from which everyone should know the answer, but I want to ask it anyway. ;P If I have an enum, like this:

    [Flags]
    public enum ThisIsAnEnum
    {
    None,
    FirstFlag,
    SecondFlag,
    ThirdFlag,
    FourthFlag
    }

    How do I test whether a specific value is used? Is this the proper way?

    ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;

    if (enumValue & ThisIsAnEnum.SecondFlag != 0)
    {
    // Do something...
    }

    Or is there some method like Flags.Match(), but for a bitfield value? And why do I have to add the values of the enum to it's members? Like this:

    [Flags]
    public enum ThisIsAnEnum
    {
    None = 0,
    FirstFlag = 1,
    SecondFlag = 2,
    ThirdFlag = 4,
    FourthFlag = 8
    }

    If I don't, their values will be 0, 1, 2, 3, 4, 5, 6..., and not 0, 1, 2, 4, 8, 16... . Thanks in advance, - Daniël Pelsmaeker


    The earth is not dying. it is being killed...

    H E 2 Replies Last reply
    0
    • U User 260964

      This is propably a question from which everyone should know the answer, but I want to ask it anyway. ;P If I have an enum, like this:

      [Flags]
      public enum ThisIsAnEnum
      {
      None,
      FirstFlag,
      SecondFlag,
      ThirdFlag,
      FourthFlag
      }

      How do I test whether a specific value is used? Is this the proper way?

      ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;

      if (enumValue & ThisIsAnEnum.SecondFlag != 0)
      {
      // Do something...
      }

      Or is there some method like Flags.Match(), but for a bitfield value? And why do I have to add the values of the enum to it's members? Like this:

      [Flags]
      public enum ThisIsAnEnum
      {
      None = 0,
      FirstFlag = 1,
      SecondFlag = 2,
      ThirdFlag = 4,
      FourthFlag = 8
      }

      If I don't, their values will be 0, 1, 2, 3, 4, 5, 6..., and not 0, 1, 2, 4, 8, 16... . Thanks in advance, - Daniël Pelsmaeker


      The earth is not dying. it is being killed...

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Daniël Pelsmaeker wrote: Is this the proper way? ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;if (enumValue & ThisIsAnEnum.SecondFlag != 0){ // Do something...} Close:

      ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;
      if ((enumValue & ThisIsAnEnum.SecondFlag) != 0)
      {
      // Do Something...
      }

      Putting the bitwise & statement in its own pair of parens is necessary due to operator precedence.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      U 1 Reply Last reply
      0
      • H Heath Stewart

        Daniël Pelsmaeker wrote: Is this the proper way? ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;if (enumValue & ThisIsAnEnum.SecondFlag != 0){ // Do something...} Close:

        ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;
        if ((enumValue & ThisIsAnEnum.SecondFlag) != 0)
        {
        // Do Something...
        }

        Putting the bitwise & statement in its own pair of parens is necessary due to operator precedence.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        U Offline
        U Offline
        User 260964
        wrote on last edited by
        #3

        Thank you very much. - Daniël Pelsmaeker


        "The secret of a successful restaurant is sharp knives." - George Orwell [pseud. of Eric Arthur Blair]

        1 Reply Last reply
        0
        • U User 260964

          This is propably a question from which everyone should know the answer, but I want to ask it anyway. ;P If I have an enum, like this:

          [Flags]
          public enum ThisIsAnEnum
          {
          None,
          FirstFlag,
          SecondFlag,
          ThirdFlag,
          FourthFlag
          }

          How do I test whether a specific value is used? Is this the proper way?

          ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;

          if (enumValue & ThisIsAnEnum.SecondFlag != 0)
          {
          // Do something...
          }

          Or is there some method like Flags.Match(), but for a bitfield value? And why do I have to add the values of the enum to it's members? Like this:

          [Flags]
          public enum ThisIsAnEnum
          {
          None = 0,
          FirstFlag = 1,
          SecondFlag = 2,
          ThirdFlag = 4,
          FourthFlag = 8
          }

          If I don't, their values will be 0, 1, 2, 3, 4, 5, 6..., and not 0, 1, 2, 4, 8, 16... . Thanks in advance, - Daniël Pelsmaeker


          The earth is not dying. it is being killed...

          E Offline
          E Offline
          Eric Gunnerson msft
          wrote on last edited by
          #4

          [Flags] is merely an attribute that's put on enums - it doesn't change the compiler behavior. We don't automatically switch to powers of two because people often want to define combinations of flags, and what was going on would be much less clear if that was the case. Ditto for looking at the numeric values someplace and having to translate back to the identifiers.

          U 1 Reply Last reply
          0
          • E Eric Gunnerson msft

            [Flags] is merely an attribute that's put on enums - it doesn't change the compiler behavior. We don't automatically switch to powers of two because people often want to define combinations of flags, and what was going on would be much less clear if that was the case. Ditto for looking at the numeric values someplace and having to translate back to the identifiers.

            U Offline
            U Offline
            User 260964
            wrote on last edited by
            #5

            Okay, but what use is the Flagsattribute then? If I'm not mistaken, I can add enum values together using |, even when I didn't set the Flagsattribute.

            E 1 Reply Last reply
            0
            • U User 260964

              Okay, but what use is the Flagsattribute then? If I'm not mistaken, I can add enum values together using |, even when I didn't set the Flagsattribute.

              E Offline
              E Offline
              Eric Gunnerson msft
              wrote on last edited by
              #6

              [Flags] isn't really a compiler thing. It's used by object browsers to tell people what kind of enumeration it is, and Enum.ToString() bases its behavior on whether [Flags] is present.

              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