C Board  

Go Back   C Board > General Programming Boards > C# Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 05-13-2008, 07:27 AM   #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
George2 is offline  
Old 05-13-2008, 07:39 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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;
Quote:

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

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.
matsp is offline  
Old 05-13-2008, 02:41 PM   #3
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
Quote:
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.
Magos is offline  
Old 05-15-2008, 05:58 AM   #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
George2 is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22