Jump to content
  • Advertisement
Sign in to follow this  
Geronimo2000

[.net] Losing type information with enums

This topic is 5128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I desperately need variables that hold enum values to retain their type information like every other variable in my program. Whenever I have to upcast to an object, the reported type is System.Object with no underlying types, and if I query for the type using GetType(), I see that the type is "Class + Enum". The plus sign is weird in the syntax. Any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
This is because an enum is simply an integer. It's not a specific data type in the same sense as a struct or class. While working on the compiler, I also discovered it doesn't even retain access specifiers (a private enum in an assembly can be imported and used in another assembly, the framework won't stop you).

Matt

Share this post


Link to post
Share on other sites
Yeah, an enum is only half a type ... there is a type entry for it, but when being used, you are using instances of type "Enum" and there is a sub-type that is their type.

In fact, to convert from a string to an enum value, you do not do:

MyEnumType.Parse(stringValue)

you do:

Enum.Parse(typeof(MyEnumType), stringValue)

in fact if you look closely you will see that the enum has primarily static functions NOT virtual functions ... so it isn't a base class for other classes, but instead of utility class itself.

Share this post


Link to post
Share on other sites
Oh, I see. I managed to work around the problem this time, but it was an interesting excercise none the less. All the advice in this thread was very helpful and straightforward - I appreciate the help.

Share this post


Link to post
Share on other sites

you could make your enum inherit a type, i do this a lot to ease serialization,

enum bleh : byte
{
bleh_zero=0,
bleh_one
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!