Enum extenting

R

Ralfeus

Hi all
Oftenly I need to use enums but standard C# enums provide little bit
poor possibilities. For example I'd like to restrict variables to get
only values specified in enums. Also sometimes I want to implement
some type conversion from enum to some other type.
Is it possible to create a new type, which would extend an enum?
Thanks
 
J

Jon Skeet [C# MVP]

Oftenly I need to use enums but standard C# enums provide little bit
poor possibilities. For example I'd like to restrict variables to get
only values specified in enums. Also sometimes I want to implement
some type conversion from enum to some other type.
Is it possible to create a new type, which would extend an enum?

No - C# enums are pretty limited.

Personally I think they should be a lot more OO - see
http://msmvps.com/blogs/jon.skeet/archive/2006/01/05/classenum.aspx
for more details about this.

Jon
 
G

Guest

First thing first, pl undestand that enums are by definition as enumerations
by EnumBuilder whos elements have correct type.

Secondly, if you have to induce special functionality to enums, you can
achieve them as said by Jon Skeet. Take a class which is a type of enum and
then carry on to implement the functionality.

Finally, apart of the above, i always recommend to write your won custom
class to spit your requirement. So that you know how you have tailored your
class, instead of depending on the BCL. For more explanation and help, post
back your requirement in detail.

HTH
 
J

jehugaleahsa

First thing first, pl undestand that enums are by definition as enumerations
by EnumBuilder whos elements have correct type.

EnumBuilder? He is just asking if there is a way to make an enum value-
safe. He doesn't want to have people passing in integers instead of
predefined values.
Secondly, if you have to induce special functionality to enums, you can
achieve them as said by Jon Skeet. Take a class which is a type of enum and
then carry on to implement the functionality.

If you are going to reference something, provide a means of looking
into it, like a link.

Here you go:
http://msmvps.com/blogs/jon.skeet/archive/2006/01/05/classenum.aspx

This should help you out.
Finally, apart of the above, i always recommend to write your won custom
class to spit your requirement. So that you know how you have tailored your
class, instead of depending on the BCL. For more explanation and help, post
back your requirement in detail.

Finally, not everyone knows what BCL (Base Class Library) stands for.
His post was detailed enough, you seem to have understood him fine;
you are just being anal.

Long ago, there was a wise man who said that the best way to make a
type-safe enum was to make a new class for each enum value and have a
polymorphic method that performed what the enum was meant to
distinguish. I was part of a large enum discussion (this question
comes up a lot) for C++. Most people would recommend the State Pattern
or making your enums polymorphic classes. Personally, I think there is
a point where being "too careful" is really just a waste of time.
Trying to prevent illegal values is like admitting that you or your
coworkers can't play by the rules and that your language isn't type-
safe. In other words, if you all promise not to pass integers where
enums belong, you'll be fine. I mean, the BCL uses enums all over and
they will often overlook illegal values (unless they throw an
exception due to bad state).

I hope you are still tracking this post. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top