473,747 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do enum values req explicit conversions

If a class inherits from another class, say Form inherits from control, then
I can assign the Form to a variable of type Control without needing an
explicit conversion, eg

Form1 f = new Form1();
Control c = f;

An enum value inherits from int but it doesn't get implicitly converted:

HorizontalAlign ment h = HorizontalAlign ment.Center;
int i = h;

Any reason?

Thanks,
Michael
Nov 17 '05 #1
31 3615
The difference is that Form is a subclass of Control. A basic rule of
inheritance (what is that guy's name again?) is that the subclass can always
be substituted for the superclass.

The problem is that an enum is not a subclass of an int (or long or sbyte or
uint, etc..). At run time, enums are treated as boxed types of the
underlying enum type. That is why you must unbox it to get the integer
value.

HTH

Dale Preston
MCAD, MCDBA, MCSE
"Michael C" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:uO******** ******@TK2MSFTN GP14.phx.gbl...
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an
explicit conversion, eg

Form1 f = new Form1();
Control c = f;

An enum value inherits from int but it doesn't get implicitly converted:

HorizontalAlign ment h = HorizontalAlign ment.Center;
int i = h;

Any reason?

Thanks,
Michael

Nov 17 '05 #2
enums are boxed at run time? That doesn't seem right to me. (But I
could be wrong. :)

I was going to say that it's more a question of self-documenting code.
One really ought to treat an enumeration as a self-contained type, even
though it is, underneath it all, just an int (or whatever). So, if you
want to start manipulating it as if it were a number, you have to cast
in order to raise a flag to the reader: "I'm doing something unusual
here! Pay attention!"

Other than that I can't see any reason.

Nov 17 '05 #3
From the C# specification:

At run-time, a value of type System.Enum can be null or a reference to a
boxed value of any enum type.

Dale

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
enums are boxed at run time? That doesn't seem right to me. (But I
could be wrong. :)

I was going to say that it's more a question of self-documenting code.
One really ought to treat an enumeration as a self-contained type, even
though it is, underneath it all, just an int (or whatever). So, if you
want to start manipulating it as if it were a number, you have to cast
in order to raise a flag to the reader: "I'm doing something unusual
here! Pay attention!"

Other than that I can't see any reason.

Nov 17 '05 #4
"Dale Preston" <da******@nospa m.nospam> wrote in message
news:uV******** ******@TK2MSFTN GP14.phx.gbl...
From the C# specification:

At run-time, a value of type System.Enum can be null or a reference to a
boxed value of any enum type.


Are you confusing the enum itself with the elements in it? (It's confusing
me a little). The actual enum inherits from enum but the elements don't as
far as I can tell. On the other hand it's possible to set one of the
elements to a variable of type System.Enum implicitly.

Michael
Nov 17 '05 #5
The enum elements inherit from System.Enum.
"Michael C" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:#G******** ******@TK2MSFTN GP14.phx.gbl...
"Dale Preston" <da******@nospa m.nospam> wrote in message
news:uV******** ******@TK2MSFTN GP14.phx.gbl...
From the C# specification:

At run-time, a value of type System.Enum can be null or a reference to a
boxed value of any enum type.


Are you confusing the enum itself with the elements in it? (It's confusing
me a little). The actual enum inherits from enum but the elements don't as
far as I can tell. On the other hand it's possible to set one of the
elements to a variable of type System.Enum implicitly.

Michael

Nov 17 '05 #6
Another thing to keep in mind, enums can use, as the underlying type (not
the base type, only the numeric representation type) an integer, long, byte,
uint, short, ushort, etc

You can't expect to implicitly convert to an int.

Dale

"Michael C" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:#G******** ******@TK2MSFTN GP14.phx.gbl...
"Dale Preston" <da******@nospa m.nospam> wrote in message
news:uV******** ******@TK2MSFTN GP14.phx.gbl...
From the C# specification:

At run-time, a value of type System.Enum can be null or a reference to a
boxed value of any enum type.


Are you confusing the enum itself with the elements in it? (It's confusing
me a little). The actual enum inherits from enum but the elements don't as
far as I can tell. On the other hand it's possible to set one of the
elements to a variable of type System.Enum implicitly.

Michael

Nov 17 '05 #7
"Dale Preston" <da******@nospa m.nospam> wrote in message
news:OM******** ******@tk2msftn gp13.phx.gbl...
The enum elements inherit from System.Enum.
I suspected this might be the case even though the object browser says it's
not. So it is inherited from Enum and NOT from int? It can be explicitly
converted to int but it's not actually an int? It's a bit misleading the
syntax then:

Public Enum ABC : int
{
....
}
Another thing to keep in mind, enums can use, as the underlying type (not
the base type, only the numeric representation type) an integer, long,
byte,
uint, short, ushort, etc

You can't expect to implicitly convert to an int.


There's no reason it shouldn't be possible

Michael
Nov 17 '05 #8

"Michael C" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
"Dale Preston" <da******@nospa m.nospam> wrote in message
news:OM******** ******@tk2msftn gp13.phx.gbl...
The enum elements inherit from System.Enum.
I suspected this might be the case even though the object browser says
it's not. So it is inherited from Enum and NOT from int? It can be
explicitly converted to int but it's not actually an int? It's a bit
misleading the syntax then:

Public Enum ABC : int
{
...
}


It is, but what other way is there that doesn't cause extra complexity?

You can't expect to implicitly convert to an int.


There's no reason it shouldn't be possible


Explicit conversion is required for consistency(Tec hnically you are losing
information, however litttle and easily regained) and to avoid making silly
mistakes.

0 has an implicit conversion to any enum, all other literals and enums must
be converted.
Nov 17 '05 #9

"Dale Preston" <da******@nospa m.nospam> wrote in message
news:uV******** ******@TK2MSFTN GP14.phx.gbl...
From the C# specification:

At run-time, a value of type System.Enum can be null or a reference to a
boxed value of any enum type.
That is because System.Enum *IS* a reference type, while derivatives are
value types. You will see a similar dichotomy in System.ValueTyp e. A value
of any given enum is just a value on the stack(literally the base type as I
don't believe enums have any other fields), but when put into a System.Enum
variable they must be boxed.

Dale

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
enums are boxed at run time? That doesn't seem right to me. (But I
could be wrong. :)

I was going to say that it's more a question of self-documenting code.
One really ought to treat an enumeration as a self-contained type, even
though it is, underneath it all, just an int (or whatever). So, if you
want to start manipulating it as if it were a number, you have to cast
in order to raise a flag to the reader: "I'm doing something unusual
here! Pay attention!"

Other than that I can't see any reason.


Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
4849
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
12
3424
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and * obvious to me. Unless I assign values to the enumerators, the * compiler will do it for me....
18
11368
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
34
11197
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
4
2699
by: jonpb | last post by:
The documentation for the "is" keyword says: An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown. So, given: enum TestEnum { t1, t2 } TestEnum te = TestEnum.t1;
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9522
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9307
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8233
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6067
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4588
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.