Tutorial :Associating enums with strings in C#



Question:

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?


Solution:1

I like to use properties in a class instead of methods, since they look more enum-like.

Here's a example for a Logger:

public class LogCategory  {   private LogCategory(string value) { Value = value; }     public string Value { get; set; }     public static LogCategory Trace { get { return new LogCategory("Trace"); } }   public static LogCategory Debug { get { return new LogCategory("Debug"); } }   public static LogCategory Info { get { return new LogCategory("Info"); } }   public static LogCategory Warning { get { return new LogCategory("Warning"); } }   public static LogCategory Error { get { return new LogCategory("Error"); } }  }  

Pass in type-safe string values as a parameter:

public static void Write(string message, LogCategory logCategory)  {     var log = new LogEntry { Message = message };     Logger.Write(log, logCategory.Value);  }  

Usage:

Logger.Write("This is almost like an enum.", LogCategory.Info);  


Solution:2

You could also use the extension model:

public enum MyEnum  {      [Description("String 1")]      V1= 1,      [Description("String 2")]      V2= 2  }   

Your Extension Class

public static class MyEnumExtensions  {      public static string ToDescriptionString(this MyEnum val)      {          DescriptionAttribute[] attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);          return attributes.Length > 0 ? attributes[0].Description : string.Empty;      }  }   

usage:

MyEnum myLocal = MyEnum.V1;  print(myLocal.ToDescriptionString());  


Solution:3

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     }    }  


Solution:4

You can add attributes to the items in the enumeration and then use reflection to get the values from the attributes.

You would have to use the "field" specifier to apply the attributes, like so:

enum GroupTypes  {      [field:Description("OEM")]      TheGroup,        [field:Description("CMB")]      TheOtherGroup  }  

You would then reflect on the static fields of the type of the enum (in this case GroupTypes) and get the DescriptionAttribute for the value you were looking for using reflection:

public static DescriptionAttribute GetEnumDescriptionAttribute<T>(      this T value) where T : struct  {      // The type of the enum, it will be reused.      Type type = typeof(T);        // If T is not an enum, get out.      if (!type.IsEnum)           throw new InvalidOperationException(              "The type parameter T must be an enum type.");        // If the value isn't defined throw an exception.      if (!Enum.IsDefined(type, value))          throw new InvalidEnumArgumentException(              "value", Convert.ToInt32(value), type);        // Get the static field for the value.      FieldInfo fi = type.GetField(value.ToString(),           BindingFlags.Static | BindingFlags.Public);        // Get the description attribute, if there is one.      return fi.GetCustomAttributes(typeof(DescriptionAttribute), true).          Cast<DescriptionAttribute>().SingleOrDefault();  }  

I opted to return the DescriptionAttribute itself above, in the event that you want to be able to determine whether or not the attribute is even applied.


Solution:5

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.


Solution:6

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.


Solution:7

Use a class.

Edit: Better example

class StarshipType  {      private string _Name;      private static List<StarshipType> _StarshipTypes = new List<StarshipType>();        public static readonly StarshipType Ultralight = new StarshipType("Ultralight");      public static readonly StarshipType Light = new StarshipType("Light");      public static readonly StarshipType Mediumweight = new StarshipType("Mediumweight");      public static readonly StarshipType Heavy = new StarshipType("Heavy");      public static readonly StarshipType Superheavy = new StarshipType("Superheavy");        public string Name      {          get { return _Name; }          private set { _Name = value; }      }        public static IList<StarshipType> StarshipTypes      {          get { return _StarshipTypes; }      }        private StarshipType(string name, int systemRatio)      {          Name = name;          _StarshipTypes.Add(this);      }        public static StarshipType Parse(string toParse)      {          foreach (StarshipType s in StarshipTypes)          {              if (toParse == s.Name)                  return s;          }          throw new FormatException("Could not parse string.");      }  }  


Solution:8

Here is the extension method that I used to get the enum value as string. First here is the enum.

public enum DatabaseEnvironment  {      [Description("AzamSharpBlogDevDatabase")]      Development = 1,       [Description("AzamSharpBlogQADatabase")]      QualityAssurance = 2,       [Description("AzamSharpBlogTestDatabase")]       Test = 3  }  

The Description attribute came from System.ComponentModel.

And here is my extension method:

public static string GetValueAsString(this DatabaseEnvironment environment)   {      // get the field       var field = environment.GetType().GetField(environment.ToString());      var customAttributes = field.GetCustomAttributes(typeof (DescriptionAttribute), false);        if(customAttributes.Length > 0)      {          return (customAttributes[0] as DescriptionAttribute).Description;        }      else      {          return environment.ToString();       }  }  

Now, you can access the enum as string value using the following code:

[TestFixture]  public class when_getting_value_of_enum  {      [Test]      public void should_get_the_value_as_string()      {          Assert.AreEqual("AzamSharpBlogTestDatabase",DatabaseEnvironment.Test.GetValueAsString());        }  }  


Solution:9

Try adding constants to a static class. You do not end up with a Type, but you have readable, organised constants:

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


Solution:10

Another way to deal with the problem, is to have a enum and a array of strings that will map the enum values with the list of strings:

public enum GroupTypes  {      TheGroup  = 0,      TheOtherGroup   }    string[] GroupTypesStr = {      "OEM",      "CMB"  };  

you may use it something like this:

Log.Write(GroupTypesStr[(int)GroupTypes.TheOtherGroup]);  

It will prompt CMB

PROS:

  1. Easy and clean code.
  2. High Performance (specially in comparison with those approaches that uses classes)

CONS:

  1. Prone to mess up the list when editing it, but it will be okay for a short list.


Solution:11

Have you considered a lookup table using a Dictionary?

enum GroupTypes  {      TheGroup,      TheOtherGroup  }    Dictionary<string, GroupTypes> GroupTypeLookup = new Dictionary<string, GroupTypes>();  // initialize lookup table:  GroupTypeLookup.Add("OEM", TheGroup);  GroupTypeLookup.Add("CMB", TheOtherGroup);  

You can then use GroupTypeLookup.TryGetValue() to look up a string when you read it.


Solution:12

Answer by Even:

public class LogCategory  {   private LogCategory(string value) { Value = value; }     public string Value { get; set; }     public static LogCategory Trace { get { return new LogCategory("Trace"); } }   public static LogCategory Debug { get { return new LogCategory("Debug"); } }   public static LogCategory Info { get { return new LogCategory("Info"); } }   public static LogCategory Warning { get { return new LogCategory("Warning"); } }   public static LogCategory Error { get { return new LogCategory("Error"); } }  }  

Just wanted to add a way how to mimic switch with class based enums:

public void Foo(LogCategory logCategory){          var @switch = new Dictionary<LogCategory, Action>{      {LogCategory.Trace, ()=>Console.Writeline("Trace selected!")},      {LogCategory.Debug, ()=>Console.Writeline("Debug selected!")},      {LogCategory.Error, ()=>Console.Writeline("Error selected!")}};       //will print one of the line based on passed argument    @switch[logCategory]();  }  


Solution:13

C# doesn't support enumerated strings, but for most situations you can use a List or Dictionary to get the desired effect.

E.g. To print pass/fail results:

List<string> PassFail = new List<string> { "FAIL", "PASS" };  bool result = true;  Console.WriteLine("Test1: " + PassFail[result.GetHashCode()]);  


Solution:14

I would make it into a class an avoid an enum altogether. And then with the usage of a typehandler you could create the object when you grab it from the db.

IE:

public class Group  {      public string Value{ get; set; }      public Group( string value ){ Value = value; }       public static Group TheGroup() { return new Group("OEM"); }      public static Group OtherGroup() { return new Group("CMB"); }    }  


Solution:15

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.


Solution:16

My first question - Do you have access to the Database itself? This should be normalized in the database, ideally, otherwise, any solution is going to be prone to error. In my experience, data fields full of "OEM" and "CMB" tend to wind up having things like "oem " and other 'crap data' mixed in over time.... If you can normalize it, you could use the key in the table containing the elements as your Enum, and you're done, with a much cleaner structure.

If that's not available, I'd make your Enum, and make a class to parse your string into the Enum for you. This would at least give you some flexibility in handling non-standard entries and much more flexibility for trapping or handling errors than doing any of the workarounds using Enum.Parse/Reflection/etc. A dictionary would work, but could break down if you ever have case issues, etc.

I'd recommend writing a class so you can do:

// I renamed this to GroupType, since it sounds like each element has a single type...  GroupType theType = GroupTypeParser.GetGroupType(theDBString);  

This preserves most of your readability without having to change the DB.


Solution:17

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.


Solution:18

If you're trying to make your code readable:

class GroupTypes {      public static final String (whatever oem stands for) = "OEM";      public static final String (whatever cmb stands for) = "CMB";      ...  }  

and if you need a list of them, include these finals in a static final List<String>. This example is in Java.

If you're trying to make your application readable, add:

public static final Map<String, String> groupsByDbValue;  static {      groupsByDbValue = new HashMap<String, String>();      groupsByDbValue.put("OEM", "(whatever OEM stands for)");      groupsByDbValue.put("CMB", "(whatever CMB stands for)");  }  


Solution:19

enums in C# are restricted to underlying integer numeric types (byte, sbyte, short, ushort, int, uint, long, and ulong). You can't associate them with a character or string based underlying value.

A different approach might be to define a dictionary of type Dictionary<string, string>.


Solution:20

In VS 2015, you can use nameof

public class LogCategory  {      public static string Trace;      public static string Debug;      public static string Info;      public static string Warning;      public static string Error;  }  

Usage:

Logger.Write("This is almost like an enum.", nameof(LogCategory.Info));  


Solution:21

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");  }  


Solution:22

You can use two enums. One for the database and the other for readability.

You just need to make sure they stay in sync, which seems like a small cost. You don't have to set the values, just set the positions the same, but setting the values makes it very clear the two enums are related and prevents errors from rearranging the enum members. And a comment lets the maintenance crew know these are related and must be kept in sync.

// keep in sync with GroupTypes  public enum GroupTypeCodes  {      OEM,      CMB  }    // keep in sync with GroupTypesCodes  public enum GroupTypes  {      TheGroup = GroupTypeCodes.OEM,      TheOtherGroup = GroupTypeCodes.CMB  }  

To use it you just convert to the code first:

GroupTypes myGroupType = GroupTypes.TheGroup;  string valueToSaveIntoDatabase = ((GroupTypeCodes)myGroupType).ToString();  

Then if you want to make it even more convenient you can add an extension function that only works for this type of enum:

public static string ToString(this GroupTypes source)  {      return ((GroupTypeCodes)source).ToString();  }  

and you can then just do:

GroupTypes myGroupType = GroupTypes.TheGroup;  string valueToSaveIntoDatabase = myGroupType.ToString();  


Solution:23

A small tweak to Glennular Extension method, so you could use the extension on other things than just ENUM's;

using System;  using System.ComponentModel;  namespace Extensions {      public static class T_Extensions {          /// <summary>          /// Gets the Description Attribute Value          /// </summary>          /// <typeparam name="T">Entity Type</typeparam>          /// <param name="val">Variable</param>          /// <returns>The value of the Description Attribute or an Empty String</returns>          public static string Description<T>(this T t) {              DescriptionAttribute[] attributes = (DescriptionAttribute[])t.GetType().GetField(t.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);              return attributes.Length > 0 ? attributes[0].Description : string.Empty;          }      }  }  

Or Using Linq

using System;  using System.ComponentModel;  using System.Linq;    namespace Extensions {      public static class T_Extensions {          public static string Description<T>(this T t) =>              ((DescriptionAttribute[])t              ?.GetType()              ?.GetField(t?.ToString())              ?.GetCustomAttributes(typeof(DescriptionAttribute), false))              ?.Select(a => a?.Description)              ?.FirstOrDefault()               ?? string.Empty;        }  }  


Solution:24

I was basically looking for the Reflection answer by @ArthurC

Just to extend his answer a little bit, you can make it even better by having a generic function:

    // If you want for a specific Enum      private static string EnumStringValue(GroupTypes e)      {          return EnumStringValue<GroupTypes>(e);      }        // Generic      private static string EnumStringValue<T>(T enumInstance)      {          return Enum.GetName(typeof(T), enumInstance);      }   

Then you can just wrap whatever you have

EnumStringValue(GroupTypes.TheGroup) // if you incorporate the top part  

or

EnumStringValue<GroupTypes>(GroupTypes.TheGroup) // if you just use the generic  


Solution:25

public class DataType  {      private readonly string value;      private static readonly Dictionary<string, DataType> predefinedValues;        public static readonly DataType Json = new DataType("json");      public static readonly DataType Xml = new DataType("xml");      public static readonly DataType Text = new DataType("text");      public static readonly DataType Html = new DataType("html");      public static readonly DataType Binary = new DataType("binary");        static DataType()      {          predefinedValues = new Dictionary<string, DataType>();          predefinedValues.Add(Json.Value, Json);          predefinedValues.Add(Xml.Value, Xml);          predefinedValues.Add(Text.Value, Text);          predefinedValues.Add(Html.Value, Html);          predefinedValues.Add(Binary.Value, Binary);      }        private DataType(string value)      {          this.value = value;      }        public static DataType Parse(string value)      {          var exception = new FormatException($"Invalid value for type {nameof(DataType)}");          if (string.IsNullOrEmpty(value))              throw exception;            string key = value.ToLower();          if (!predefinedValues.ContainsKey(key))              throw exception;            return predefinedValues[key];      }        public string Value      {          get { return value; }      }  }  


Solution:26

Based in other opinions, this is what I come up with. This approach avoids having to type .Value where you want to get the constant value.

I have a base class for all string enums like this:

using System;  using Newtonsoft.Json;    [JsonConverter(typeof(ConstantConverter))]  public class StringEnum: IConvertible  {      public string Value { get; set; }        protected StringEnum(string value)      {          Value = value;      }        public static implicit operator string(StringEnum c)      {          return c.Value;      }      public string ToString(IFormatProvider provider)      {          return Value;      }        public TypeCode GetTypeCode()      {          throw new NotImplementedException();      }        public bool ToBoolean(IFormatProvider provider)      {          throw new NotImplementedException();      }      //The same for all the rest of IConvertible methods  }  

The JsonConverter is like this:

using System;  using Newtonsoft.Json;    class ConstantConverter : JsonConverter  {      public override bool CanConvert(Type objectType)      {          return true;      }        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)      {          throw new NotImplementedException();      }        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)      {          if (value == null)          {              serializer.Serialize(writer, null);          }          else          {              serializer.Serialize(writer, value.ToString());          }      }  }  

And an actual string enum will be something like this:

public sealed class Colors : StringEnum  {      public static Colors Red { get { return new Catalog("Red"); } }      public static Colors Yellow { get { return new Catalog("Yellow"); } }      public static Colors White { get { return new Catalog("White"); } }        private Colors(string value) : base(value) { }  }  

And with this, you can just use Color.Red to even serialize to json without using the Value property


Solution:27

I even implemented a few enums as suggested by @Even (via class X and public static X members), just to find out later that these days, starting .Net 4.5, there's the right ToString() method.

Now I'm reimplementing everything back to enums.


Solution:28

I didn't need anything robust like storing the string in attributes. I just needed to turn something like MyEnum.BillEveryWeek into "bill every week" or MyEnum.UseLegacySystem into "use legacy system"--basically split the enum by its camel-casing into individiual lower-case words.

public static string UnCamelCase(this Enum input, string delimiter = " ", bool preserveCasing = false)  {      var characters = input.ToString().Select((x, i) =>      {           if (i > 0 && char.IsUpper(x))         {             return delimiter + x.ToString(CultureInfo.InvariantCulture);         }         return x.ToString(CultureInfo.InvariantCulture);        });        var result = preserveCasing         ? string.Concat(characters)         : string.Concat(characters).ToLower();        var lastComma = result.LastIndexOf(", ", StringComparison.Ordinal);        if (lastComma > -1)      {         result = result.Remove(lastComma, 2).Insert(lastComma, " and ");      }        return result;  }  

MyEnum.UseLegacySystem.UnCamelCase() outputs "use legacy system"

If you have multiple flags set, it will turn that into plain english (comma-delimited except an "and" in place of the last comma).

var myCustomerBehaviour = MyEnum.BillEveryWeek | MyEnum.UseLegacySystem | MyEnum.ChargeTaxes;    Console.WriteLine(myCustomerBehaviour.UnCamelCase());  //outputs "bill every week, use legacy system and charge taxes"  


Solution:29

Why not just use ToString() on the Enum types?

private enum LogType { Error, Warning, Info};    private WriteLog(string message, LogType logType)  {      Log.Write(message, logType.ToString());  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »