Thread: basic questions about enum

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    basic questions about enum

    Hello everyone,


    I have 3 basic questions about Enum, from MSDN,

    http://msdn.microsoft.com/en-us/libr...32(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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Hello everyone,


    I have 3 basic questions about Enum, from MSDN,

    http://msdn.microsoft.com/en-us/libr...32(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;
    No, I'm pretty sure you can assign Days d = 1234;

    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?
    I think this is related to something like this:
    Code:
    enum e { a = 1, b = 8, c = 12 };
    ... // in a different module
    e x;
    switch(x)
    {
       case a:
          ... 
       case b:
         ... 
       default:
          ... 
    }
    Now, if we add a "d" to the range, d = 7, then unexpected things may happen.

    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?
    Essentially, it converts the enum to be have bit values instead of single values. If you or together SunRoof and FogLights, the value is 5, and in normal mode, that isn't part of the enum value range, so the decimal value 5 is printed. In FlagsAttribute mode, it splits out the bits of the enum and shows the SunRoof and FogLights as separate units.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    No, I'm pretty sure you can assign Days d = 1234;
    Nope, you can't. You could however do Days d = (Days)1234; with an exception being thrown if 1234 has no mathing value in Days.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    1.

    I have written a test program which shows whether or not using Flags attribute, the result value is the same, the presentation form is different when using Console.write method.

    Code:
    enum e1 { a,b,c,d};
    
    [Flags]
    enum e2 { a, b, c, d };
    
    static void Main(string[] args)
    {
    e1 MyE1 = e1.a | e1.b;
    e2 MyE2 = e2.a | e2.b;
    // e1 == e2
    return;
    }
    So, the conclusion is, value the same, but presentation form is differnet? Right?

    2.

    If yes, besides Console.write, any other presentation forms which will show the differences when using Flags attribute or not?

    Quote Originally Posted by matsp View Post
    Essentially, it converts the enum to be have bit values instead of single values. If you or together SunRoof and FogLights, the value is 5, and in normal mode, that isn't part of the enum value range, so the decimal value 5 is printed. In FlagsAttribute mode, it splits out the bits of the enum and shows the SunRoof and FogLights as separate units.

    --
    Mats

    I do not agree, Magos! :-)


    It is ok to me. Could you show your complete test code please?

    Quote Originally Posted by Magos View Post
    Nope, you can't. You could however do Days d = (Days)1234; with an exception being thrown if 1234 has no mathing value in Days.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some basic C questions
    By WarDoGG in forum C Programming
    Replies: 5
    Last Post: 07-12-2008, 10:40 AM
  2. Stacks and enum questions
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 04-25-2005, 06:39 PM
  3. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  4. Please help, basic unsigned conversion questions
    By ninjacookies in forum C Programming
    Replies: 3
    Last Post: 04-20-2005, 10:50 AM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM