Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try using a enum and the <code>DescriptionAttribute</code>:</p> <pre><code>public enum REASON_CODES { [Description("10")] HumanReadableReason1, [Description("11")] SomethingThatWouldMakeSense, /* etc. */ } </code></pre> <p>Then you can use a helper (like <a href="http://leandrob.com/2009/07/enumgetdescription-extension-method/" rel="nofollow noreferrer"><code>Enumerations.GetDescription</code></a>) to get the "real" value (while staying within the C# naming constraints).</p> <p>To make it a full-out answer:</p> <ul> <li>@BryanRowe wrote an Enum-to-Description method<br> <a href="https://stackoverflow.com/a/1799401/298053">https://stackoverflow.com/a/1799401/298053</a></li> <li>@max wrote a Description-to-Enum method<br> <a href="https://stackoverflow.com/a/4367868/298053">https://stackoverflow.com/a/4367868/298053</a></li> </ul> <hr> <p>Just in case anyone wants an extensions class that marries those two posters:</p> <pre><code>public static class EnumExtensions { public static String ToDescription&lt;TEnum&gt;(this TEnum e) where TEnum : struct { var type = typeof(TEnum); if (!type.IsEnum) throw new InvalidOperationException("type must be an enum"); var memInfo = type.GetMember(e.ToString()); if (memInfo != null &amp; memInfo.Length &gt; 0) { var descAttr = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); if (descAttr != null &amp;&amp; descAttr.Length &gt; 0) { return ((DescriptionAttribute)descAttr[0]).Description; } } return e.ToString(); } public static TEnum ToEnum&lt;TEnum&gt;(this String description) where TEnum : struct { var type = typeof(TEnum); if (!type.IsEnum) throw new InvalidOperationException("type must be an enum"); foreach (var field in type.GetFields()) { var descAttr = field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (descAttr != null &amp;&amp; descAttr.Length &gt; 0) { if (((DescriptionAttribute)descAttr[0]).Description == description) { return (TEnum)field.GetValue(null); } } else if (field.Name == description) { return (TEnum)field.GetValue(null); } } return default(TEnum); // or throw new Exception(); } } </code></pre> <p>Then:</p> <pre><code>public enum CODES { [Description("11")] Success, [Description("22")] Warning, [Description("33")] Error } // to enum String response = "22"; CODES responseAsEnum = response.ToEnum&lt;CODES&gt;(); // CODES.Warning // from enum CODES status = CODES.Success; String statusAsString = status.ToDescription(); // "11" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload