Enum is not clear

Discussion in 'Microsoft Dot NET Framework Forms' started by Alexander Vasilevsky, Dec 25, 2008.

  1. Something I did not understand the joke humor
    What's the magic of 0 in enums?

    class Program
    {
    static void Main(string[] args)
    {
    Foo a_obj = new Foo();
    SubjectStatus a_s = a_obj.Status;
    Console.WriteLine(a_s + "-" + a_obj.Status);
    a_obj.Status = SubjectStatus.New;
    Console.WriteLine(a_obj.Status);
    a_obj.Status = 0;
    Console.WriteLine(a_obj.Status);
    // a_obj.Status = 3;
    // Console.WriteLine(a_obj.Status);
    }
    }

    public class Foo
    {
    private SubjectStatus m_Status;

    public virtual SubjectStatus Status
    {
    get { return m_Status; }
    set { m_Status = value; }
    }
    }
    /// <summary>
    /// Subject Status
    /// </summary>
    public enum SubjectStatus
    {
    New = 3,
    Actual = 5,
    Acrhive = 6
    }
    And why, if the comment to remove the program does not compile? do not see
    the logic.

    http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
    Gift
     
    Alexander Vasilevsky, Dec 25, 2008
    #1
    1. Advertisements

  2. Alexander Vasilevsky

    Jack Jackson Guest

    On Thu, 25 Dec 2008 11:20:52 +0200, "Alexander Vasilevsky"
    <> wrote:

    >Something I did not understand the joke humor
    >What's the magic of 0 in enums?
    >
    > class Program
    > {
    > static void Main(string[] args)
    > {
    > Foo a_obj = new Foo();
    > SubjectStatus a_s = a_obj.Status;
    > Console.WriteLine(a_s + "-" + a_obj.Status);
    > a_obj.Status = SubjectStatus.New;
    > Console.WriteLine(a_obj.Status);
    > a_obj.Status = 0;
    > Console.WriteLine(a_obj.Status);
    >// a_obj.Status = 3;
    >// Console.WriteLine(a_obj.Status);
    > }
    > }
    >
    > public class Foo
    > {
    > private SubjectStatus m_Status;
    >
    > public virtual SubjectStatus Status
    > {
    > get { return m_Status; }
    > set { m_Status = value; }
    > }
    > }
    > /// <summary>
    > /// Subject Status
    > /// </summary>
    > public enum SubjectStatus
    > {
    > New = 3,
    > Actual = 5,
    > Acrhive = 6
    > }
    >And why, if the comment to remove the program does not compile? do not see
    >the logic.


    Here is a comment (quoted at
    <http://cleveralias.blogs.com/thought_spearmints/2004/01/more_c_enum_wac.html>)
    by Eric Gunnerson of the C# development team:

    Enums in C# do dual purpose. They are used for the usual enum use,
    and they're also used for bit fields. When I'm dealing with bit
    fields, you often want to AND a value with the bit field and check if
    it's true.

    Our initial rules meant that you had to write:

    if ((myVar & MyEnumName.ColorRed) != (MyEnumName) 0)

    which we thought was difficult to read. One alernative was to
    define a zero entry:

    if ((myVar & MyEnumName.ColorRed) != MyEnumName.NoBitsSet)

    which was also ugly.

    We therefore decided to relax our rules a bit, and permit an
    implicit conversion from the literal zero to any enum type, which
    allows you to write:

    if ((myVar & MyEnumName.ColorRed) != 0)
     
    Jack Jackson, Dec 25, 2008
    #2
    1. Advertisements

  3. Alexander Vasilevsky

    Jeff Johnson Guest

    "Alexander Vasilevsky" <> wrote in message
    news:givj9k$gvc$...

    > Something I did not understand the joke humor
    > What's the magic of 0 in enums?


    Any variable of an enum type can be set to 0 without explicitly casting 0 to
    that type of enum. Any other value must be cast.
     
    Jeff Johnson, Dec 29, 2008
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd

    How do I find the flags of an enum?

    Todd, Aug 4, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    0
    Views:
    232
  2. Justin Weinberg

    Shortcut Enum

    Justin Weinberg, Sep 2, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    3
    Views:
    383
    Herfried K. Wagner [MVP]
    Sep 3, 2003
  3. Marauderz

    BUG : RotateFlipType Enum Doesn't Populate In PropertyGrid

    Marauderz, Sep 26, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    0
    Views:
    334
    Marauderz
    Sep 26, 2003
  4. Guest

    Casting of a Object* to an Enum value??

    Guest, Feb 2, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    2
    Views:
    188
    Guest
    Feb 3, 2004
  5. Guest

    Property to accept only an enum type

    Guest, Oct 22, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    1
    Views:
    168
    joeycalisay
    Oct 22, 2004
Loading...

Share This Page