Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to have userfriendly names for enumerations?
    text
    copied!<p>I have an enumeration like </p> <pre><code>Enum Complexity { NotSoComplex, LittleComplex, Complex, VeryComplex } </code></pre> <p>And I want to use it in a dropdown list, but don't want to see such Camel names in list (looks really odd for users). Instead I would like to have in normal wording, like Not so complex Little complex (etc)</p> <p>Also, my application is multi-lang and I would like to be able to display those strings localized, and I use a helper, TranslationHelper(string strID) which gives me the localized version for a string id.</p> <p>I have a working solution, but not very elegant: I create a helper class for the enum, with one member Complexity and ToString() overwritten, like below (code simplified)</p> <pre><code>public class ComplexityHelper { public ComplexityHelper(Complexity c, string desc) { m_complex = c; m_desc=desc; } public Complexity Complexity { get { ... } set {...} } public override ToString() { return m_desc; } //Then a static field like this private static List&lt;Complexity&gt; m_cxList = null; // and method that returns the status lists to bind to DataSource of lists public static List&lt;ComplexityHelper&gt; GetComplexities() { if (m_cxList == null) { string[] list = TranslationHelper.GetTranslation("item_Complexities").Split(','); Array listVal = Enum.GetValues(typeof(Complexities)); if (list.Length != listVal.Length) throw new Exception("Invalid Complexities translations (item_Complexities)"); m_cxList = new List&lt;Complexity&gt;(); for (int i = 0; i &lt; list.Length; i++) { Complexity cx = (ComplexitylistVal.GetValue(i); ComplexityHelper ch = new ComplexityHelper(cx, list[i]); m_cxList.Add(ch); } } return m_cxList; } } </code></pre> <p>While workable, I'm not happy with it, since I have to code it similarily for various enums I need to use in picklists.</p> <p>Does anyone have a suggestion for a simpler or more generic solution?</p> <p>Thanks Bogdan</p>
 

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