Possible to have strings for enums?

using System.ComponentModel;   
enum FilterType
{
    [Description("Rigid")]
    Rigid,
    [Description("Soft / Glow")]
    SoftGlow,
    [Description("Ghost")]
    Ghost ,
}

You can get the value out like this

public static String GetEnumerationDescription(Enum e)
{
  Type type = e.GetType();
  FieldInfo fieldInfo = type.GetField(e.ToString());
  DescriptionAttribute[] da = (DescriptionAttribute[])(fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false));
  if (da.Length > 0)
  {
    return da[0].Description;
  }
  return e.ToString();
}

No, but if you want to scope "const" strings and use them like an enum, here's what I do:

public static class FilterType
{
   public const string Rigid = "Rigid";
   public const string SoftGlow =  "Soft / Glow";
   public const string Ghost ="Ghost";
}

No, but you can cheat like this:

public enum FilterType{
   Rigid,
   SoftGlow,
   Ghost
}

And then when you need their string values you can just do FilterType.Rigid.ToString().

Tags:

C#

.Net

Enums