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. enums

enums

Scheduled Pinned Locked Moved C#
tutorial
12 Posts 5 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.
  • G Offline
    G Offline
    Gilbert Consellado
    wrote on last edited by
    #1

    for example i have

    [System.Flags]
    public enum PrescriptionTypePrint
    {
    PrescriptionOnly = 1,
    InstructionOnly = 2,
    Both = PrescriptionOnly | InstructionOnly
    }

    how i could make it simplify replacing the || to | on the code below

    PrescriptionTypePrint pp = PrescriptionTypePrint.Both;

    if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
    {
    //execute this block
    }

    becuase i think this would not work

    if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
    {
    //execute this block
    }
    // the goal of this if pp has a value of prescription or both it will execute.

    thank you so much for the some one who shed me some light

    V OriginalGriffO L B 4 Replies Last reply
    0
    • G Gilbert Consellado

      for example i have

      [System.Flags]
      public enum PrescriptionTypePrint
      {
      PrescriptionOnly = 1,
      InstructionOnly = 2,
      Both = PrescriptionOnly | InstructionOnly
      }

      how i could make it simplify replacing the || to | on the code below

      PrescriptionTypePrint pp = PrescriptionTypePrint.Both;

      if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
      {
      //execute this block
      }

      becuase i think this would not work

      if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
      {
      //execute this block
      }
      // the goal of this if pp has a value of prescription or both it will execute.

      thank you so much for the some one who shed me some light

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      I would do this:

      [System.Flags]
      public enum PrescriptionTypePrint
      {
      PrescriptionOnly = 1,
      InstructionOnly = 2,
      //Both = PrescriptionOnly | InstructionOnly
      }

      and then:

      if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.InstructionOnly))
      {
      //execute this block
      }

      I think that should work. In any case using "both" as naming convention is a bit confusing. I'm also not sure about the behaviour of the both definition in the enum definition.

      V.
      (MQOTD rules and previous solutions)

      G 1 Reply Last reply
      0
      • V V 0

        I would do this:

        [System.Flags]
        public enum PrescriptionTypePrint
        {
        PrescriptionOnly = 1,
        InstructionOnly = 2,
        //Both = PrescriptionOnly | InstructionOnly
        }

        and then:

        if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.InstructionOnly))
        {
        //execute this block
        }

        I think that should work. In any case using "both" as naming convention is a bit confusing. I'm also not sure about the behaviour of the both definition in the enum definition.

        V.
        (MQOTD rules and previous solutions)

        G Offline
        G Offline
        Gilbert Consellado
        wrote on last edited by
        #3

        i dont know if using the name "both" is the right name to use, but it will act as it has the value of "PrescriptionOnly" and "InstructionONly"

        1 Reply Last reply
        0
        • G Gilbert Consellado

          for example i have

          [System.Flags]
          public enum PrescriptionTypePrint
          {
          PrescriptionOnly = 1,
          InstructionOnly = 2,
          Both = PrescriptionOnly | InstructionOnly
          }

          how i could make it simplify replacing the || to | on the code below

          PrescriptionTypePrint pp = PrescriptionTypePrint.Both;

          if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
          {
          //execute this block
          }

          becuase i think this would not work

          if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
          {
          //execute this block
          }
          // the goal of this if pp has a value of prescription or both it will execute.

          thank you so much for the some one who shed me some light

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          If you think of it in terms of binary:

          PrescriptionOnly = 01
          InstructionOnly = 10
          Both = 11

          And you want either PrescriptionOnly or Both:

          01 or 11

          So, of the four possible states the variable can be in, you want two:

          None 00 X
          PrescriptionOnly 01 Y
          InstructionOnly 10 X
          Both 11 Y

          So all you have to do is:

          if ((pp & PrescriptionTypePrint.PrescriptionOnly) != 0)
          {
          ...

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • G Gilbert Consellado

            for example i have

            [System.Flags]
            public enum PrescriptionTypePrint
            {
            PrescriptionOnly = 1,
            InstructionOnly = 2,
            Both = PrescriptionOnly | InstructionOnly
            }

            how i could make it simplify replacing the || to | on the code below

            PrescriptionTypePrint pp = PrescriptionTypePrint.Both;

            if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
            {
            //execute this block
            }

            becuase i think this would not work

            if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
            {
            //execute this block
            }
            // the goal of this if pp has a value of prescription or both it will execute.

            thank you so much for the some one who shed me some light

            L Offline
            L Offline
            lukeer
            wrote on last edited by
            #5

            Use another binary operator

            if( (pp & PrescriptionTypePrint.Both) != 0)
            {
            // execute this block
            }

            This will binary-AND two integer operands. The result will equal zero if the oparands don't share a common bit set to '1'. Therefore 'pp' will not result in the expression to equal zero whenever any one of the bits of 'Both' are set in 'pp'.

            Ciao, luker

            OriginalGriffO 1 Reply Last reply
            0
            • L lukeer

              Use another binary operator

              if( (pp & PrescriptionTypePrint.Both) != 0)
              {
              // execute this block
              }

              This will binary-AND two integer operands. The result will equal zero if the oparands don't share a common bit set to '1'. Therefore 'pp' will not result in the expression to equal zero whenever any one of the bits of 'Both' are set in 'pp'.

              Ciao, luker

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Um...that'll also execute it for the InstructionOnly case, won't it?

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Um...that'll also execute it for the InstructionOnly case, won't it?

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                L Offline
                L Offline
                lukeer
                wrote on last edited by
                #7

                Yep, it will. Your questioning seems like that's not what you wanted. Looking again at what you wrote, I think that's what you need:

                if( ( pp & PrescriptionOnly ) != 0 )
                {
                // execute in case of PrescriptionOnly or Both
                // Since Both sets, among others, the PrescriptionOnly-bit.
                }

                Ciao, luker

                OriginalGriffO 1 Reply Last reply
                0
                • L lukeer

                  Yep, it will. Your questioning seems like that's not what you wanted. Looking again at what you wrote, I think that's what you need:

                  if( ( pp & PrescriptionOnly ) != 0 )
                  {
                  // execute in case of PrescriptionOnly or Both
                  // Since Both sets, among others, the PrescriptionOnly-bit.
                  }

                  Ciao, luker

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Um...you mean what the OP wanted? That is what I had already suggested... :laugh:

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  L 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Um...you mean what the OP wanted? That is what I had already suggested... :laugh:

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    L Offline
                    L Offline
                    lukeer
                    wrote on last edited by
                    #9

                    Sorry, Griff. Yes, I meant the OP. And Wow, your answer is exhaustive. I'll give it an up.

                    Ciao, luker

                    G 1 Reply Last reply
                    0
                    • L lukeer

                      Sorry, Griff. Yes, I meant the OP. And Wow, your answer is exhaustive. I'll give it an up.

                      Ciao, luker

                      G Offline
                      G Offline
                      Gilbert Consellado
                      wrote on last edited by
                      #10

                      opsss, I miss the conversation, sorry i was out for a few days. By the way THANK YOU SO MUCH for the suggestions, surely I will try it.

                      1 Reply Last reply
                      0
                      • G Gilbert Consellado

                        for example i have

                        [System.Flags]
                        public enum PrescriptionTypePrint
                        {
                        PrescriptionOnly = 1,
                        InstructionOnly = 2,
                        Both = PrescriptionOnly | InstructionOnly
                        }

                        how i could make it simplify replacing the || to | on the code below

                        PrescriptionTypePrint pp = PrescriptionTypePrint.Both;

                        if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
                        {
                        //execute this block
                        }

                        becuase i think this would not work

                        if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
                        {
                        //execute this block
                        }
                        // the goal of this if pp has a value of prescription or both it will execute.

                        thank you so much for the some one who shed me some light

                        B Offline
                        B Offline
                        BillWoodruff
                        wrote on last edited by
                        #11

                        A slightly different approach using Exclusive-Or:

                        [Flags]
                        enum testEnum
                        {
                        p1 = 1,
                        p2 = 2,
                        p3 = 3,
                        p4 = 4,
                        p5 = 5
                        }

                        private void TestExOr()
                        {
                        testEnum t1 = testEnum.p3;

                        testEnum t2 = testEnum.p5;
                        
                        // true
                        bool test1 = t1 == (testEnum.p1 ^ testEnum.p2);
                        bool test2 = t2 == (testEnum.p1 ^ testEnum.p4);
                        bool test3 = t2 == (testEnum.p0 ^ testEnum.p5);
                        
                        // false
                        bool test4 = t1 == (testEnum.p2 ^ testEnum.p3);
                        bool test5 = t2 == (testEnum.p2 ^ testEnum.p3);
                        bool test6 = t2 == (testEnum.p5 ^ testEnum.p4);
                        bool test7 = t2 == (testEnum.p5 ^ testEnum.p1);
                        
                        // is the false result here a deal-breaker ?
                        bool test8 = t2 == (testEnum.p5 ^ testEnum.p5);}
                        

                        «A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push»  Wittgenstein

                        G 1 Reply Last reply
                        0
                        • B BillWoodruff

                          A slightly different approach using Exclusive-Or:

                          [Flags]
                          enum testEnum
                          {
                          p1 = 1,
                          p2 = 2,
                          p3 = 3,
                          p4 = 4,
                          p5 = 5
                          }

                          private void TestExOr()
                          {
                          testEnum t1 = testEnum.p3;

                          testEnum t2 = testEnum.p5;
                          
                          // true
                          bool test1 = t1 == (testEnum.p1 ^ testEnum.p2);
                          bool test2 = t2 == (testEnum.p1 ^ testEnum.p4);
                          bool test3 = t2 == (testEnum.p0 ^ testEnum.p5);
                          
                          // false
                          bool test4 = t1 == (testEnum.p2 ^ testEnum.p3);
                          bool test5 = t2 == (testEnum.p2 ^ testEnum.p3);
                          bool test6 = t2 == (testEnum.p5 ^ testEnum.p4);
                          bool test7 = t2 == (testEnum.p5 ^ testEnum.p1);
                          
                          // is the false result here a deal-breaker ?
                          bool test8 = t2 == (testEnum.p5 ^ testEnum.p5);}
                          

                          «A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push»  Wittgenstein

                          G Offline
                          G Offline
                          Gilbert Consellado
                          wrote on last edited by
                          #12

                          Thank you so much I will try it.

                          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