Adding Functionality to Enum Type

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Given the extreme object orientation of .Net I am wondering if it is possible to add a functionality to an Enum Type. I have the simple Enum type:
VB.NET:
  public enum MyEnum
    value1
    value2
    value3
  end enum

But, given the declarative nature of enum types I can't do:
VB.NET:
  public enum myenum
    public shared function DoSomething() as string
    end function
  end enum

The compiler disagrees with this, so I'm wondering if I can inherit from the Enum class and write my own functionality to it from which I can expand the behavior of the enum type.

thanks
Jaeden "Sifo Dyas" al'Raec Ruiner
 
No, Enum is just a simple list, you can only put constant values there.
 
The point of an Enum is to give user-friendly labels to a numeric list. The system manipulates the numeric values, which is most efficient for the computer, while the developer refers to them by their labels, which is most efficient for a human being. The other advantage over using just integer or string values is that valid values for the type are restricted to just those declared in the list, while an Integer variable can be any integer and a String variable can be any string.

Why would you want to do what you're asking for anyway? What you are talking about is already supported by classes and structures, so why would enumerated types need to support it too?
 
The point of an Enum is to give user-friendly labels to a numeric list. The system manipulates the numeric values, which is most efficient for the computer, while the developer refers to them by their labels, which is most efficient for a human being. The other advantage over using just integer or string values is that valid values for the type are restricted to just those declared in the list, while an Integer variable can be any integer and a String variable can be any string.
For the record I began 17 years ago with Assembler for the 80386, so I know why and how things work. Vb is sorely lacking in a lot of things, but it works for my purposes now.

Enum apparently is one of those 'bastard' types that programming languages are prone to invent to hack the standard they created, yet they never conform to their own rules. If enum is an Object, then I should be able to Inherit from the Enum base type, and thus declare my own Enum sub-class type that has added functionality. But since what you say decrees this isn't possible, then Enum is NOT truly and Object, and it is just another HACK that so many languages use to achieve a illusion of a standard.

The conversion of the string "Value1" to the value MyEnum.Value1 is built in to the enum type but it's somewhat of a pain in the ass to write. MyEnum.ToString() works, but they didn't create a similar shared function MyEnum.FromString(). There does exist the "Parse()" function, which I basically created a structured function to handle for me. But I figured it was effectually excessive when It should be a shared function built in to the enum type, which is what I wanted to do.

Jaeden "Sifo Dyas" al'Raec Ruiner
 
Enumerations defined by the Enum keyword are value types, they inherit behind the scenes from System.Enum class which in turn inherits ValueType class (neither of which we are allowed to touch). The Parse method of System.Enum class is shared, ToString is an instance method, you probably just mixed the terms, why these methods are defined like this also makes sense in OOP. I don't understand what you are saying about "writing MyEnum.Value1", you never write enum values when they are used, you just select them from the list the IDE provides you. It is what the IDE does for you when using enums that make this special type unique, if not there wouldn't be much need for it, one could have used a class containing constants instead (still more typing/copy-paste though, less user-friendly, less readable - and not type safe). As of VB9 you can actually extend an enum type also, with something called "extension methods".
 
Back
Top