Associating enums with strings in C#

2019-01-02 16:54发布

I know the following is not possible because it has to be an int

enum GroupTypes
{
    TheGroup = "OEM",
    TheOtherGroup = "CMB"
}

From my database I get a field with incomprehensive codes (the OEM and CMB's). I would want to make this field into an enum or something else understandable. Because the target is readability the solution should be terse.
What other options do I have?

标签: c# .net
30条回答
时光乱了年华
2楼-- · 2019-01-02 17:02

How about using a static class with constants? The client code will look no different from enums.

static class GroupTypes
{
  public const string TheGroup = "OEM";
  public const string TheOtherGroup = "CMB";
}

void DoSomething(GroupTypes groupType)
{
  if(groupType == GroupTypes.TheOtherGroup)
  {
    //Launch nuclear bomb 
  }  
}
查看更多
牵手、夕阳
3楼-- · 2019-01-02 17:02

Create a second enum, for your DB containing the following:

enum DBGroupTypes
{
    OEM = 0,
    CMB = 1
}

Now, you can use Enum.Parse to retrieve the correct DBGroupTypes value from the strings "OEM" and "CMB". You can then convert those to int and retrieve the correct values from the right enumeration you want to use further in your model.

查看更多
后来的你喜欢了谁
4楼-- · 2019-01-02 17:02

If I understand correctly, you need a conversion from string to enum:

enum GroupTypes {
    Unknown = 0,
    OEM = 1,
    CMB = 2
}
static GroupTypes StrToEnum(string str){
    GroupTypes g = GroupTypes.Unknown;
    try {
        object o = Enum.Parse(typeof(GroupTypes), str, true);
        g = (GroupTypes)(o ?? 0);
    } catch {
    }
    return g;
}
// then use it like this
GroupTypes g1 = StrToEnum("OEM");
GroupTypes g2 = StrToEnum("bad value");

You can make it more fancy with generics for the enum type if you wish.

查看更多
初与友歌
5楼-- · 2019-01-02 17:02

This is a way to use it as a strongly typed parameter or as a string :

public class ClassLikeEnum
{
    public string Value
    {
        get;
        private set;
    }

    ClassLikeEnum(string value) 
    {
        Value = value;
    }

    public static implicit operator string(ClassLikeEnum c)
    {
        return c.Value;
    }

    public static readonly ClassLikeEnum C1 = new ClassLikeEnum("RandomString1");
    public static readonly ClassLikeEnum C2 = new ClassLikeEnum("RandomString2");
}
查看更多
不流泪的眼
6楼-- · 2019-01-02 17:03

You can do it very easily actually. Use the following code.

enum GroupTypes
{
   OEM,
   CMB
};

Then when you want to get the string value of each enum element just use the following line of code.

String oemString = Enum.GetName(typeof(GroupTypes), GroupTypes.OEM);

I've used this method successfully in the past, and I've also used a constants class to hold string constants, both work out pretty well, but I tend to prefer this.

查看更多
永恒的永恒
7楼-- · 2019-01-02 17:03

I would just create a dictionary and use the code as the key.

Edit: To address the comment about doing a reverse lookup (finding the key), this would not be terribly efficient. If this is necessary, I would write a new class to handle it.

查看更多
登录 后发表回答