3

I need to convert a List of enums values to a single string to store in my db; then convert back again when I retrieve from the database.

Each enum's value is currently a simple integer, so it feels a bit overkill to create an extra table to deal with this.

So, in the example below, if the user selects Mother, Father and Sister, then the value stored in the database will be "0,1,3"

  public enum MyEnum
    {
        Mother = 0,
        Father = 1,
        Gran = 2,
        Sister = 3,
        Brother = 4
    }

I'm new to c#, so not sure if there is a nice out-the-box way to do this - I couldn't find anything obvious when google hunting!

Cheers in advance :) - L

laura
  • 2,951
  • 9
  • 44
  • 61

8 Answers8

10

Enum's are explicitely able to be cast to/from integers

int value = (int)MyEnum.Mother;

and

   MyEnum value = (MyEnum)1;

For strings use ToString and Enum.Parse

string value = MyEnum.Mother.ToString();

and

MyEnum value = (MyEnum)Enum.Parse(typeof(MyEnum),"Mother");
Jamiec
  • 133,658
  • 13
  • 134
  • 193
4

If you change you enum values to:

[Flags]
public enum MyEnum
    {
        Mother = 1,
        Father = 2,
        Gran = 4,
        Sister = 8,
        Brother = 16,
    }

Then you could store Father and Gran as 6

Sister and Brother as 24 etc

by using binary numbers you should not get duplicate values by combining them

WraithNath
  • 17,658
  • 10
  • 55
  • 82
3

Just use ToString to convert to the name, and the use Enum.TryParse (or Enum.Parse if you're not on .NET 4) to convert back.

If you're wanting one enum field to contain multiple values (e.g MyEnum.Mother | MyEnum.Father), you'll need to convert that to a Flags enum, as @WraithNath suggested. Otherwise you're talking about storing each option separately (there's no way to store Mother and Father in the same field with your current setup).

Community
  • 1
  • 1
bdukes
  • 152,002
  • 23
  • 148
  • 175
3

from your code it is

MyEnum a = MyEnum.Mother;
string thestring = a.ToString();
MyEnum b = (MyEnum) Enum.Parse(typeof(MyEnum), thestring);
John Kenedy
  • 564
  • 1
  • 5
  • 18
3

The following will convert back and forth between an array of Enum values via "0,1,3" as requested:

MyEnum[] selection = { MyEnum.Mother, MyEnum.Father, MyEnum.Sister };

string str = string.Join(",", selection.Cast<int>());

MyEnum[] enm = str.Split(',').Select(s => int.Parse(s)).Cast<MyEnum>().ToArray();
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
1

String Equivelant

MyEnum value = MyEnum.Father;
value.ToString(); // prints Father

Parsing

(MyEnum)Enum.Parse(typeof(MyEnum), "Father"); // returns MyEnum.Father
Ian
  • 33,605
  • 26
  • 118
  • 198
0

Enums can be cast to and from integer values. That's probably your best bet.

If you really want to use strings, ToString will return "Mother" "Father" "Gran" etc. Casting back from a string would just be a function:

private MyEnum GetEnumValue(string fromDB)
{
    if( fromDB == "Mother" ) return MyEnum.Mother;
    else if( fromDB == "Father") return MyEnum.Father;
    //etc. etc.
}

EDIT: Ian's answer for casting back is the more "C#-ey" way of doing it, and is far more extensible (handles adding new values to the enum much more flexibly).

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111
0

Further to Jamiec's suggestion (which fits well for your need), if you need to defensively code your casting, try:

if (Enum.IsDefined(typeof(MyEnum), <your database value>))
{
    // Safe to convert your enumeration at this point:
    MyEnum value = (MyEnum)1;   
}
Shan Plourde
  • 8,528
  • 2
  • 29
  • 42