Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Basic Friendly names</strong></p> <p>Use the <a href="http://blogs.msdn.com/abhinaba/archive/2005/10/20/c-enum-and-overriding-tostring-on-it.aspx" rel="noreferrer">Description attribute:</a>*</p> <pre><code>enum MyEnum { [Description("This is black")] Black, [Description("This is white")] White } </code></pre> <p>And a handy extension method for enums:</p> <pre><code>public static string GetDescription(this Enum value) { FieldInfo field = value.GetType().GetField(value.ToString()); object[] attribs = field.GetCustomAttributes(typeof(DescriptionAttribute), true); if(attribs.Length &gt; 0) { return ((DescriptionAttribute)attribs[0]).Description; } return string.Empty; } </code></pre> <p>Used like so:</p> <pre><code>MyEnum val = MyEnum.Black; Console.WriteLine(val.GetDescription()); //writes "This is black" </code></pre> <p>(Note this doesn't exactly work for bit flags...)</p> <p><strong>For localization</strong></p> <p>There is a well-established pattern in .NET for handling multiple languages per string value - use a <a href="http://www.codeproject.com/KB/dotnet/Localization.aspx" rel="noreferrer">resource file</a>, and expand the extension method to read from the resource file:</p> <pre><code>public static string GetDescription(this Enum value) { FieldInfo field = value.GetType().GetField(value.ToString()); object[] attribs = field.GetCustomAttributes(typeof(DescriptionAttribute), true)); if(attribs.Length &gt; 0) { string message = ((DescriptionAttribute)attribs[0]).Description; return resourceMgr.GetString(message, CultureInfo.CurrentCulture); } return string.Empty; } </code></pre> <p>Any time we can leverage existing BCL functionality to achieve what we want, that's definitely the first route to explore. This minimizes complexity and uses patterns already familiar to many other developers.</p> <p><strong>Putting it all together</strong></p> <p>To get this to bind to a DropDownList, we probably want to track the real enum values in our control and limit the translated, friendly name to visual sugar. We can do so by using an anonymous type and the DataField properties on the list:</p> <pre><code>&lt;asp:DropDownList ID="myDDL" DataTextField="Description" DataValueField="Value" /&gt; myDDL.DataSource = Enum.GetValues(typeof(MyEnum)).OfType&lt;MyEnum&gt;().Select( val =&gt; new { Description = val.GetDescription(), Value = val.ToString() }); myDDL.DataBind(); </code></pre> <p>Let's break down that DataSource line:</p> <ul> <li>First we call <code>Enum.GetValues(typeof(MyEnum))</code>, which gets us a loosely-typed <code>Array</code> of the values</li> <li>Next we call <code>OfType&lt;MyEnum&gt;()</code> which converts the array to an <code>IEnumerable&lt;MyEnum&gt;</code></li> <li>Then we call <code>Select()</code> and provide a lambda that projects a new object with two fields, Description and Value.</li> </ul> <p>The DataTextField and DataValueField properties are evaluated reflectively at databind-time, so they will search for fields on DataItem with matching names.</p> <p>-<em>Note in the main article, the author wrote their own <code>DescriptionAttribute</code> class which is unnecessary, as one already exists in .NET's standard libraries.</em>-</p>
    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. 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