I found a goodlooking example about implementation enums in a different way. That is called type-safe enum pattern i think. I started using it but i realized th
Jordão has the right idea, but there is a better way to implement the polymorphism, use delegate.
The use of delegates is faster than a switch statement. (In fact, I am a strong believer that the only place for switch statements in object-oriented development is in a factory method. I always look for some sort of polymorphism to replace any switch statements in any code i deal with.)
For example, if you want a specific behavior based on a type-safe-enum, the following pattern is what I use:
public sealed class EnumExample
{
#region Delegate definitions
///
/// This is an example of adding a method to the enum.
/// This delegate provides the signature of the method.
///
/// A parameter for the delegate
/// Specifies the return value, in this case a (possibly
/// different) EnumExample
private delegate EnumExample DoAction(string input);
#endregion
#region Enum instances
///
/// Description of the element
/// The static readonly makes sure that there is only one immutable
/// instance of each.
///
public static readonly EnumExample FIRST = new EnumExample(1,
"Name of first value",
delegate(string input)
{
// do something with input to figure out what state comes next
return result;
}
);
...
#endregion
#region Private members
///
/// The string name of the enum
///
private readonly string name;
///
/// The integer ID of the enum
///
private readonly int value;
///
/// The method that is used to execute Act for this instance
///
private readonly DoAction action;
#endregion
#region Constructors
///
/// This constructor uses the default value for the action method
///
/// Note all constructors are private to prevent creation of instances
/// by any other code
///
/// integer id for the enum
/// string value for the enum
private EnumExample(int value, string name)
: this (value, name, defaultAction)
{
}
///
/// This constructor sets all the values for a single instance.
/// All constructors should end up calling this one.
///
/// the integer ID for the enum
/// the string value of the enum
/// the method used to Act
private EnumExample(int value, string name, DoAction action)
{
this.name = name;
this.value = value;
this.action = action;
}
#endregion
#region Default actions
///
/// This is the default action for the DoAction delegate
///
/// The inpute for the action
/// The next Enum after the action
static private EnumExample defaultAction(string input)
{
return FIRST;
}
#endregion
...
}