Nathan J Pledger

Program.X musings from the Isle of Man concerning ASP.NET, in particular accessibility, web standards and neat ideas.

Wouldn't you like to see: Inherited Enumerations?

Came across this idea today and I do say it owuld be rather useful.

I have a series of classes that provide different types of offer for products, based on a shopping basket.

I have an enumeration that identifies the result of an offer being applied:

public enum MediaCodeOfferResult { OfferApplied,
OrderDoesNotQualify,
UnknownError,
MediaCodeOfferError,
MediaCodeOfferTypeError,
AssemblyNotLoaded,
TypeNotLoaded,
TypeNotCastable }

But I would like to use the same enumeration to identify, with more granularity, what went wrong in each of the classes. eg.:

public enum MediaCodeOfferXForYResult { InsufficientInput } : MediaCodeOfferResult;

Wouldn't this be useful?

I can't see that it would be that difficult to do. An enum is just an int, after all. I find them really useful for quick and dirty result codes, like this, that don't need frameworks to be built to return values that can be accounted for and reacted against.

 

Comments

Erwyn van der Meer said:

It would be difficult to do. You can't inherit from System.Int32 either. In fact, you cannot inherit from any valuetype in .NET. This is by design. Valuetypes can live "unboxed" on the stack and in that case don't carry a type identifier for the instance. Allowing inheritance would mean you could substitute an instance of a class derived from a valuetype for that valuetype. If you would be able to override a method in the derived class the runtime would have a hard time figuring out which method to call because it can't see if it is dealing with an instance of the parent class or an instance of the derived class. Or in other words valuetypes and polymorphism don't mix very well.

Check out http://blogs.msdn.com/joelpob/archive/2004/07/19/187709.aspx for more details.

# August 20, 2006 10:04 AM

Marc Jacobi said:

YES! I've been wanting that ever since I first learned of enumerations (about 10 years ago ;-).

I think an exception could be made for the enum value type with regard to the inheritance restictions. I dont see anything wrong with inheriting from an existing enum.

Maybe its even enough to implement it as syntactic suger that just copies all the entries of the base type...

# August 21, 2006 12:51 AM

Scott Monaghan said:

I'm tackling a similar problem, and I've come up with a solution that you may or may not like.

(I use vb but you get the idea)

Public Enum StronglyTypedEnum As Int32

CommonEnum1 = 0

CommonEnum2= 1

CommonEnum3= 2

ClassSpecificEnum = 4

End Enum

Public Class BaseClass

    Public Const CommonEnum1 As StronglyTypedEnum = StronglyTypedEnum.CommonEnum1

    Public Const CommonEnum2 As StronglyTypedEnum = StronglyTypedEnum.CommonEnum2

    Public Const CommonEnum3 As StronglyTypedEnum = StronglyTypedEnum.CommonEnum3

End Class

Public Class InheritedClass Inherits BaseClass

   Public Const ClassSpecificEnum As StronglyTypedEnum = StronglyTypedEnum.StronglyTypedEnum  

End Class

Now if you we can do this:

Dim value As StronlyTypedEnum

value = InheritedClass.CommonEnum1

value = InheritedClass.CommonEnum2

value = InheritedClass.CommonEnum3

value = InheritedClass.ClassSpecificEnum

---------------------------------

Of course with this method you lose the ability  to use Enum specific methods, but if you are just after the values, as I am usually this works quite well.  The other pain is that you have to maintain the StronglyTypedEnum Enum separately, but it's a good solution if you care about strongly typed values rather than simple int32's.  

# August 25, 2006 12:53 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)