ebvtrnog
ebvtrnog

Reputation: 4377

idea to create rich enum

I have created for myself several classes of the same structure - "rich enums". So it immediately suggests that it could be somehow simplified by another class I would inherit.

I wrote an example class to show you what such an enum contains:

class RichEnum
{
    // ???
}

class MyEnum : RichEnum
{
    // FIELDS AND CONSTRUCTOR(S)
    public readonly string A;
    public readonly int B;
    public readonly Object C;

    public MyEnum(string A, int B, Object C)
    {
        this.A = A;
        this.B = B;
        this.C = C;
    }

    // STATIC INSTANCES
    public static readonly MyEnum Example1 = new MyEnum("string1", 1, "object1");
    public static readonly MyEnum Example2 = new MyEnum("string2", 2, "object2");
    public static readonly MyEnum Example3 = new MyEnum("string3", 3, "object3");

    // SPECIAL INSTANCE
    public static readonly MyEnum Default = new MyEnum("default", 0, "default");

    // SPECIAL OBJECT FOR ITERATING OVER STATIC INSTANCES
    public static readonly MyEnum[] Values = { Example1, Example2, Example3 };

    // METHODS
    public int GetSomeNumber()
    {
        return B + 10;
    }
}

I have lots of various "MyEnums", but these classes all have the same structure. So they have:

Is there a way to create one class of this structure and then just inherit from it, making the process more simplified and less error-prone?

Upvotes: 3

Views: 906

Answers (3)

John Castleman
John Castleman

Reputation: 1561

It seems you're reinventing the wheel: Headspring.Enumeration.

So, if your goal is just to have such a "rich" enumeration class available to you, there's a Nuget package for that; if your goal is to write your own ... well, it never hurts to look at prior art for inspiration.

Upvotes: 1

aquinas
aquinas

Reputation: 23786

Take a look at this code I posted on CodeProject a LONG time ago.

http://www.codeproject.com/Articles/13293/Descriptive-Enumerations

I won't post all the code, although it's not too bad, but here's an example of actually using it. In this case, I am trying to define an enum of a passenger's airline seat preference.

public class SeatType : DescriptiveEnum<SeatType, int>
{
    public static readonly SeatType Window = new SeatType("Window Seat", 1);
    public static readonly SeatType Aisle = new SeatType("Aisle Seat", 2);
    public static readonly SeatType AnythingExceptSeatNearBathroom = new SeatType("Anything Except Seat Near Bathroom", 3);

    private SeatType(string desc, int code)
        : base(desc, code)
    {
    }
}

So, as you can see, that's pretty clean. Of course, you could certainly extend this to add additional methods, or include a "default", etc.

Then you can use it like this:

class Example
{

    public static void Main(string[] args)
    {
        SeatType c = SeatType.AnythingExceptSeatNearBathroom;

        Console.WriteLine(c.Description);
        Console.WriteLine(SeatType.AnythingExceptSeatNearBathroom == c);
        foreach (SeatType seat in SeatType.GetEnumMembers())
        {
            Console.WriteLine(String.Format("Seat type code: {0} - description: {1}",seat.Code,seat.Description));
        }


        Console.ReadLine();
    }
}

The base DescriptiveEnum class also has conversion operators baked in, so it works JUST like enums do. That is, you can say:

SeatType c = (SeatType) SomeMethodThatReturnsAnInt();

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

If you really look at it, these classes have actually nothing in common that could be abstracted:

  1. They have a different amount of static and non-static fields
  2. The types of the fields are different
  3. The values of each field differ
  4. The names of the fields differ
  5. The methods differ

Unless you find something they have in common, you can't create a base class. What could help you would be a code snippet that contains the general layout of such a class to speed up your typing.

Upvotes: 0

Related Questions