PC Review


Reply
Thread Tools Rate Thread

Enum is not clear

 
 
Alexander Vasilevsky
Guest
Posts: n/a
 
      25th Dec 2008
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


 
Reply With Quote
 
 
 
 
Jack Jackson
Guest
Posts: n/a
 
      25th Dec 2008
On Thu, 25 Dec 2008 11:20:52 +0200, "Alexander Vasilevsky"
<(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
 
Jeff Johnson
Guest
Posts: n/a
 
      29th Dec 2008
"Alexander Vasilevsky" <(E-Mail Removed)> wrote in message
news:givj9k$gvc$(E-Mail Removed)...

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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialize an Enum using the Enum names and not value Chris Dunaway Microsoft C# .NET 1 27th Oct 2005 05:23 PM
Alternatives to Enum.Parse and Enum.Format ME Microsoft Dot NET 4 18th May 2005 10:07 AM
Alternatives to Enum.Parse and Enum.Format ME Microsoft Dot NET Compact Framework 4 18th May 2005 10:07 AM
Casting a value from a table to an Enum using Enum.ToObject KyleK Microsoft C# .NET 1 11th May 2004 04:07 PM
clear history, not "Clear History" or "Clear Forms" or TypedURLS Nick Windows XP Internet Explorer 4 31st Mar 2004 05:22 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:04 AM.