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. basic questions about enum

basic questions about enum

Scheduled Pinned Locked Moved C#
visual-studiocomtoolstutorialquestion
7 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.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, I have 3 basic questions about Enum, from MSDN, http://msdn.microsoft.com/en-us/library/sbbt4032(VS.80).aspx 1. It is mentioned "A variable of type Days can be assigned any value in the range of the underlying type;" -- I think it is not correct to say any value in the range of the underlying type, like integer, but in the range from Sat to Fri. For example, you can not write Days d = 65535; 2. "and if additional elements have been added to the enum type, the test for default values can return true unexpectedly." I do not quite understand this scenario, could anyone show me some code please? 3. "You can notice these changes when using tools such as the Console class methods, the Expression Evaluator, and so forth. (See example 3).", I have tried example 3. But what are the rules for the changes when we add System.FlagsAttribute? The document only says there will be some changes, but not clearly states what will be the changes. Any ideas? thanks in advance, George

    N 1 Reply Last reply
    0
    • G George_George

      Hello everyone, I have 3 basic questions about Enum, from MSDN, http://msdn.microsoft.com/en-us/library/sbbt4032(VS.80).aspx 1. It is mentioned "A variable of type Days can be assigned any value in the range of the underlying type;" -- I think it is not correct to say any value in the range of the underlying type, like integer, but in the range from Sat to Fri. For example, you can not write Days d = 65535; 2. "and if additional elements have been added to the enum type, the test for default values can return true unexpectedly." I do not quite understand this scenario, could anyone show me some code please? 3. "You can notice these changes when using tools such as the Console class methods, the Expression Evaluator, and so forth. (See example 3).", I have tried example 3. But what are the rules for the changes when we add System.FlagsAttribute? The document only says there will be some changes, but not clearly states what will be the changes. Any ideas? thanks in advance, George

      N Offline
      N Offline
      Nissim Salomon
      wrote on last edited by
      #2

      Hi In the MSDN enum was declare as follow "The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list" 1. you need to understand that at the bottem line there is a native variable like int or short that can hold other values. for example: Days d = (Days)int.MaxValue; 2. "Assigning additional values new versions of enums, or changing the values of the enum members in a new version, can cause problems for dependant source code. It is often the case that enum values are used in switch statements, and if additional elements have been added to the enum type, the test for default values can return true unexpectedly." in case that you are using flags for bitwaise operation, if one or more enum value was changed you need to update your program to prevent unwanted if results

      G 1 Reply Last reply
      0
      • N Nissim Salomon

        Hi In the MSDN enum was declare as follow "The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list" 1. you need to understand that at the bottem line there is a native variable like int or short that can hold other values. for example: Days d = (Days)int.MaxValue; 2. "Assigning additional values new versions of enums, or changing the values of the enum members in a new version, can cause problems for dependant source code. It is often the case that enum values are used in switch statements, and if additional elements have been added to the enum type, the test for default values can return true unexpectedly." in case that you are using flags for bitwaise operation, if one or more enum value was changed you need to update your program to prevent unwanted if results

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Thanks nissims, Some further questions/comments. 1. If we write an Enum variable like d in my code, and assign it beyond the related int range from Sat to Fri, does such code have any practical usage? I think people always use the values in the range and treat the ones beyond the range as invalid values. 2. I do not quite understand what do you mean "in case that you are using flags for bitwaise operation", could you show me some pseudo code please? regards, George

        N A 2 Replies Last reply
        0
        • G George_George

          Thanks nissims, Some further questions/comments. 1. If we write an Enum variable like d in my code, and assign it beyond the related int range from Sat to Fri, does such code have any practical usage? I think people always use the values in the range and treat the ones beyond the range as invalid values. 2. I do not quite understand what do you mean "in case that you are using flags for bitwaise operation", could you show me some pseudo code please? regards, George

          N Offline
          N Offline
          Nissim Salomon
          wrote on last edited by
          #4

          1.you are right the all purpose of enum is to give you the ability to use some logical const names insetd of numbers for example enum Days { Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri } vs 1,2,3,4,5,6,7 there for it is not wise to override the enum consts using numbers if the enum was declared use it, specially if you need to cast it in order to use numbers. 2.Please read the "using flags attribute" in the msdn article.

          G 1 Reply Last reply
          0
          • G George_George

            Thanks nissims, Some further questions/comments. 1. If we write an Enum variable like d in my code, and assign it beyond the related int range from Sat to Fri, does such code have any practical usage? I think people always use the values in the range and treat the ones beyond the range as invalid values. 2. I do not quite understand what do you mean "in case that you are using flags for bitwaise operation", could you show me some pseudo code please? regards, George

            A Offline
            A Offline
            Anthony Mushrow
            wrote on last edited by
            #5

            Any practical usage? There's probably something, but i can't think of anything. And on to flags:

            [Flags]
            public enum MyKeys : int
            {
            Null = 0,
            Up = 0x01,
            Down = 0x02,
            Left = 0x04,
            Right = 0x08
            }

            ...
            MyKeys keys = MyKeys.Up | MyKeys.Right;

            if((keys&MyKeys.Right) == MyKeys.Right)
            MessageBox.Show("keys contains MyKeys.Right!");

            Each value in the enum represents 1 bit:

            0x01 = 0001
            0x02 = 0010
            0x04 = 0100
            0x08 = 1000

            So if we use the bitwise operators OR and AND we can add and check for these bits. In the above code: MyKeys keys = MyKeys.Up | MyKeys.Right; is the same as MyKeys keys = 0001 | 1000; keys is now 1001 keys&MyKeys.Right is the same as 1001&1000 and the result is 1000

            My current favourite word is: Bacon!

            -SK Genius

            Game Programming articles start -here[^]-

            G 1 Reply Last reply
            0
            • A Anthony Mushrow

              Any practical usage? There's probably something, but i can't think of anything. And on to flags:

              [Flags]
              public enum MyKeys : int
              {
              Null = 0,
              Up = 0x01,
              Down = 0x02,
              Left = 0x04,
              Right = 0x08
              }

              ...
              MyKeys keys = MyKeys.Up | MyKeys.Right;

              if((keys&MyKeys.Right) == MyKeys.Right)
              MessageBox.Show("keys contains MyKeys.Right!");

              Each value in the enum represents 1 bit:

              0x01 = 0001
              0x02 = 0010
              0x04 = 0100
              0x08 = 1000

              So if we use the bitwise operators OR and AND we can add and check for these bits. In the above code: MyKeys keys = MyKeys.Up | MyKeys.Right; is the same as MyKeys keys = 0001 | 1000; keys is now 1001 keys&MyKeys.Right is the same as 1001&1000 and the result is 1000

              My current favourite word is: Bacon!

              -SK Genius

              Game Programming articles start -here[^]-

              G Offline
              G Offline
              George_George
              wrote on last edited by
              #6

              Thanks SK Genius, My current confusion is, when add or remove flags attribute, the result is always the same. So I am wondering the special usage of the attribute compared with the situation when we do not use it. Here is my test code, any comments?

              using System;

              class Test
              {
              enum Days
              {
              Mon = 1,
              Tue = 2
              };

              \[Flags\]
              enum DaysFlag
              {
                  Mon = 1,
                  Tue = 2
              };
              
              static void Main()
              {
                  int a = (int) (Days.Mon | Days.Tue);
                  int b = (int)(DaysFlag.Mon | DaysFlag.Tue);
              
                  return;
              }
              

              }

              regards, George

              1 Reply Last reply
              0
              • N Nissim Salomon

                1.you are right the all purpose of enum is to give you the ability to use some logical const names insetd of numbers for example enum Days { Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri } vs 1,2,3,4,5,6,7 there for it is not wise to override the enum consts using numbers if the enum was declared use it, specially if you need to cast it in order to use numbers. 2.Please read the "using flags attribute" in the msdn article.

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #7

                Thanks nissims, 1.

                nissims wrote:

                if the enum was declared use it, specially if you need to cast it in order to use numbers.

                I do not quite understand your above comments, could you show some pseudo code please? 2. Any comments to? http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2552145[^] regards, George

                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