.NETGURU
enum oddity...
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcs' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Ken Meads
MessageI can declare an enum like the following:

enum MyEnums {ENUM1=1, ENUM2, ENUM3, ENUM23=23, ENUM24};

...
// Somewhere else in a method...
for (MyEnums l_enum = MyEnums.ENUM1; l_enum <MyEnums.ENUM24; l_enum++)
{
Console.WriteLine("l_enum == {0}", l_enum);
}

Now, having come from a C++ POV, that wouldn't even compile because performing an increment on an enum is illegal. But, C# allows that to happen. Which would be fine if it incremented through the list. But l_enum can wind up being a value that is something other than what MyEnums contains. In this case, l_enum can have a value of 4, 5, or any value whatsoever. Also, fwiw, I cannot implicitly set the MyEnums variable to just any value. For instance, this code produces a compiler error:
MyEnums l_enum = 8;
I can cast it OK:
MyEnums l_enum = (MyEnums)8;
and that works just fine.

I understand that an enum is an int (in this case) but not really.

Does anyone else find this a little odd?
Reply to this message...
 
    
Steven A Smith (VIP)
MessageI do. Sounds like a bug to me...

Steve

----- Original Message -----
From: Ken Meads
To: aspngcs
Sent: Thursday, June 27, 2002 11:09 PM
Subject: [aspngcs] enum oddity...

I can declare an enum like the following:

enum MyEnums {ENUM1=1, ENUM2, ENUM3, ENUM23=23, ENUM24};

...
// Somewhere else in a method...
for (MyEnums l_enum = MyEnums.ENUM1; l_enum <MyEnums.ENUM24; l_enum++)
{
Console.WriteLine("l_enum == {0}", l_enum);
}

Now, having come from a C++ POV, that wouldn't even compile because performing an increment on an enum is illegal. But, C# allows that to happen. Which would be fine if it incremented through the list. But l_enum can wind up being a value that is something other than what MyEnums contains. In this case, l_enum can have a value of 4, 5, or any value whatsoever. Also, fwiw, I cannot implicitly set the MyEnums variable to just any value. For instance, this code produces a compiler error:
MyEnums l_enum = 8;
I can cast it OK:
MyEnums l_enum = (MyEnums)8;
and that works just fine.

I understand that an enum is an int (in this case) but not really.

Does anyone else find this a little odd?
| [aspngcs] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
Reply to this message...
 
    
joseph.gaus@dowcorning.com
I found this odd, so I thought I'd look into it. At first, I thought maybe
it was incrementing through the index of the Enum, so I wrote slightly
different code to test that theory. Turns out, I was wrong.

class Class1
{

static void Main(string[] args)
{
for (MyEnums l_enum = MyEnums.ENUM1; l_enum <MyEnums.ENUM4; l_enum++)
{
Console.WriteLine("l_enum == {0}", l_enum);
}
Console.ReadLine();
}

enum MyEnums {ENUM1=2, ENUM2=4, ENUM3=6, ENUM4=8};

}

The output of this code (surprisingly?), was:

l_enum == ENUM1
l_enum == 3
l_enum == ENUM2
l_enum == 5
l_enum == ENUM3
l_enum == 7

I suppose some of the implications speak for themselves. Strange though, it
seems that when it increments, it's figuring out that it's an integer and
allowing that implicitly. It even treats it that way when the integer
doesn't map to an enum value. But then when the integer does map to an enum
value, it goes back to treating it like an enum. Very unlike C# to allow
such implicit functionality.

Doing some more testing, I was able to create consistant results using the
following as an enum
enum MyEnums {ENUM1='a', ENUM2='c', ENUM3='e', ENUM4='g'};

The results simply returned ascii codes for 'b', 'd', and 'f', while writing
out ENUM1, ENUM2, and ENUM3 appropriately as before.

Apparently, any datatype that has a ++ method defined is allowed.

Looking further, about Enums, I find:
An Enum is a named constant whose underlying type is any integral type
except Char. If no underlying type is explicitly declared, Int32 is used.

Well, that explains the integers. The chars then, in my second test, must
have been converted to integers when the enum was created.

Whether or not this is a bug, I won't say, but it certainly was fun looking
into it.

Thanks,
Joe

-----Original Message-----
From: Steven A Smith [mailto:Click here to reveal e-mail address]
Sent: Friday, June 28, 2002 12:33 AM
To: aspngcs
Subject: [aspngcs] Re: enum oddity...

I do. Sounds like a bug to me...

Steve

----- Original Message -----
From: Ken <mailto:Click here to reveal e-mail address> Meads
To: aspngcs <mailto:Click here to reveal e-mail address>
Sent: Thursday, June 27, 2002 11:09 PM
Subject: [aspngcs] enum oddity...

I can declare an enum like the following:

enum MyEnums {ENUM1=1, ENUM2, ENUM3, ENUM23=23, ENUM24};

...
// Somewhere else in a method...
for (MyEnums l_enum = MyEnums.ENUM1; l_enum <MyEnums.ENUM24; l_enum++)
{
Console.WriteLine("l_enum == {0}", l_enum);
}

Now, having come from a C++ POV, that wouldn't even compile because
performing an increment on an enum is illegal. But, C# allows that to
happen. Which would be fine if it incremented through the list. But l_enum
can wind up being a value that is something other than what MyEnums
contains. In this case, l_enum can have a value of 4, 5, or any value
whatsoever. Also, fwiw, I cannot implicitly set the MyEnums variable to
just any value. For instance, this code produces a compiler error:
MyEnums l_enum = 8;
I can cast it OK:
MyEnums l_enum = (MyEnums)8;
and that works just fine.

I understand that an enum is an int (in this case) but not really.

Does anyone else find this a little odd?
| [aspngcs] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT

| [aspngcs] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________
Reply to this message...
 
    
Ken Meads
MessageI guess what bugs me is that have a variable of this type (enum in
this case) and yet it can take on values other than those that are defined.

I wouldn't imagine that it is a bug - but it would be nice to know the
philosophy behind it the decision to behave as it does.

Oh well...
<Click here to reveal e-mail address> wrote in message news:676623@aspngcs...
I found this odd, so I thought I'd look into it. At first, I thought maybe
it was incrementing through the index of the Enum, so I wrote slightly
different code to test that theory. Turns out, I was wrong.

class Class1
{

static void Main(string[] args)
{
for (MyEnums l_enum = MyEnums.ENUM1; l_enum <MyEnums.ENUM4; l_enum++)
{
Console.WriteLine("l_enum == {0}", l_enum);
}
Console.ReadLine();
}

enum MyEnums {ENUM1=2, ENUM2=4, ENUM3=6, ENUM4=8};

}

The output of this code (surprisingly?), was:

l_enum == ENUM1
l_enum == 3
l_enum == ENUM2
l_enum == 5
l_enum == ENUM3
l_enum == 7

I suppose some of the implications speak for themselves. Strange though, it
seems that when it increments, it's figuring out that it's an integer and
allowing that implicitly. It even treats it that way when the integer
doesn't map to an enum value. But then when the integer does map to an enum
value, it goes back to treating it like an enum. Very unlike C# to allow
such implicit functionality.

Doing some more testing, I was able to create consistant results using the
following as an enum
enum MyEnums {ENUM1='a', ENUM2='c', ENUM3='e', ENUM4='g'};

The results simply returned ascii codes for 'b', 'd', and 'f', while writing
out ENUM1, ENUM2, and ENUM3 appropriately as before.

Apparently, any datatype that has a ++ method defined is allowed.

Looking further, about Enums, I find:
An Enum is a named constant whose underlying type is any integral type
except Char. If no underlying type is explicitly declared, Int32 is used.

Well, that explains the integers. The chars then, in my second test, must
have been converted to integers when the enum was created.

Whether or not this is a bug, I won't say, but it certainly was fun looking
into it.

Thanks,
Joe

-----Original Message-----
From: Steven A Smith [mailto:Click here to reveal e-mail address]
Sent: Friday, June 28, 2002 12:33 AM
To: aspngcs
Subject: [aspngcs] Re: enum oddity...

I do. Sounds like a bug to me...

Steve

----- Original Message -----
From: Ken Meads
To: aspngcs
Sent: Thursday, June 27, 2002 11:09 PM
Subject: [aspngcs] enum oddity...

I can declare an enum like the following:

enum MyEnums {ENUM1=1, ENUM2, ENUM3, ENUM23=23, ENUM24};

...
// Somewhere else in a method...
for (MyEnums l_enum = MyEnums.ENUM1; l_enum <MyEnums.ENUM24; l_enum++)
{
Console.WriteLine("l_enum == {0}", l_enum);
}

Now, having come from a C++ POV, that wouldn't even compile because
performing an increment on an enum is illegal. But, C# allows that to
happen. Which would be fine if it incremented through the list. But l_enum
can wind up being a value that is something other than what MyEnums
contains. In this case, l_enum can have a value of 4, 5, or any value
whatsoever. Also, fwiw, I cannot implicitly set the MyEnums variable to
just any value. For instance, this code produces a compiler error:
MyEnums l_enum = 8;
I can cast it OK:
MyEnums l_enum = (MyEnums)8;
and that works just fine.

I understand that an enum is an int (in this case) but not really.

Does anyone else find this a little odd?
| [aspngcs] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
| [aspngcs] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

Reply to this message...
 
 
System.Console




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification