0

I'm trying to create a class that works as a flexible enum. I came with this idea, with 2 additional static methods that allow me to add new members to that list and get only members from that list.

public class LikeEnum
{
         private static List<LikeEnum> list = new List<LikeEnum>()
         { new LikeEnum("One"), new LikeEnum("Two") };

         private string value;

         private LikeEnum(string value)
         {
               this.value = value;
         }
}

Unfortunately the List list is only initialized as null so this doesn't work...

Any ideas or suggestions?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
userk
  • 169
  • 2
  • 3
  • 9
  • possible duplicate of [C#: can you add to an enum type in run-time](http://stackoverflow.com/questions/2779743/c-can-you-add-to-an-enum-type-in-run-time) – Igor Zevaka May 21 '10 at 00:45

3 Answers3

4

Unfortunately the List list is only initialized as null so this doesn't work...

No, it's not "initialized as null". It's just never initialized... The static constructor (implicit in your case) is only executed the first time a static field of the class is accessed. Since you can't create an instance (constructor is private) and there are no public static members, the class stays uninitialized...

Anyway, for what your trying to do, the List<LikeEnum> is not very useful... a better option, if you want it to be like an enum, would be to create static readonly fields :

public class LikeEnum
{
    public static readonly LikeEnum One = new LikeEnum("One");
    public static readonly LikeEnum Two = new LikeEnum("Two");

    private string value;

    private LikeEnum(string value)
    {
        this.value = value;
    }
}

EDIT : by the way, I assume you did not post the complete code of your class ? With the code you posted, there's no way to get a TypeInitializationException, since the type will never be initialized...

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • That would be a great solution if it allows me to add new "enums", and I think it doesn't.. Yes, I just post a small excerpt. – userk May 21 '10 at 00:43
  • No, it won't be possible to add new "enum values" that way... But anyway, how do you intend to use that class ? the point of enums is that the values are predefined, so it doesn't make sense to add new values to an enum at runtime – Thomas Levesque May 21 '10 at 01:08
1

heres a more common pattern for what you are trying to do, i remember it was popular in java before 1.5

public sealed class LikeEnum
{
    public static readonly LikeEnum One = new LikeEnum("one");
    public static readonly LikeEnum Two = new LikeEnum("Two");

    private string value;
    private LikeEnum(string v)
    {
        value = v;
    }
}

because the class is sealed and the constructor is private the only enums that can be created are those that you specify.

luke
  • 14,518
  • 4
  • 46
  • 57
0

Since you said enum I assume that you also want to use them in switch statements. Here is an example that allows you to do that:

public struct LikeEnum
{

         public const string One = "One";
         public const string Two = "Two";

         private readonly string value;

         private LikeEnum(string value)
         {
               this.value = value;
         }

         public static implicit operator LikeEnum(string value) {
             return new LikeEnum(value);
         }

         public static implicit operator string(LikeEnum le) {
             return le.value;
         }
}

//In the code
LikeEnum le = new LikeEnum("One");
switch(le) {
 case LikeEnum.One:
   //do something
   break;
}
Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128