<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Snipplr</title>
<link>http://snipplr.com/tags/enum</link>
<description>Recent snippets posted on Snipplr.com</description>
<language>en-us</language>
<pubDate>Sat, 25 May 2013 19:28:40 GMT</pubDate>
<item>
<title>(C#) Enum Description - dimitrisdapontes</title>
<link>http://snipplr.com/view/69657/enum-description/</link>
<description><![CDATA[ <p>Returns the description set in an enum value, or if not description is found it returns the name of the enum value. Use this without worrying about passing the enum type, you should only pass the enum value

Example

internal enum Duration
{
    [Description("Every 12 Months")] Year = 1
    [Description("Every 6 Months")] Semester = 2
}

GetEnumDescription(Duration.Year) returns "Every 12 Months"
</p> ]]></description>
<pubDate>Sat, 26 Jan 2013 21:28:06 GMT</pubDate>
<guid>http://snipplr.com/view/69657/enum-description/</guid>
</item>
<item>
<title>(VB.NET) Enum Description - dimitrisdapontes</title>
<link>http://snipplr.com/view/69656/enum-description/</link>
<description><![CDATA[ <p>Returns the description set in an enum value, or if no description is found it returns the name of the enum value. Use this without worrying about passing the enum type, you should only pass the enum value

Example

Friend Enum Duration
    &amp;lt;Description("Every 12 Months")&amp;gt; Year = 1
    &amp;lt;Description("Every 6 Months")&amp;gt; Semester = 2
End Enum

GetEnumDescription(Duration.Year) returns "Every 12 Months"
</p> ]]></description>
<pubDate>Sat, 26 Jan 2013 21:14:56 GMT</pubDate>
<guid>http://snipplr.com/view/69656/enum-description/</guid>
</item>
<item>
<title>(C#) .NET - C# - Enum - Basics - denakitan</title>
<link>http://snipplr.com/view/67826/net--c--enum--basics/</link>
<description><![CDATA[ <p>Examples of use of Enum type, like how to iterate its items.</p> ]]></description>
<pubDate>Sat, 20 Oct 2012 04:49:18 GMT</pubDate>
<guid>http://snipplr.com/view/67826/net--c--enum--basics/</guid>
</item>
<item>
<title>(VB.NET) Bind Enum to Combobox - Maikel</title>
<link>http://snipplr.com/view/53011/bind-enum-to-combobox/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Wed, 04 May 2011 23:53:49 GMT</pubDate>
<guid>http://snipplr.com/view/53011/bind-enum-to-combobox/</guid>
</item>
<item>
<title>(C#) Bind Enum to Combobox - Maikel</title>
<link>http://snipplr.com/view/53010/bind-enum-to-combobox/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Wed, 04 May 2011 23:52:54 GMT</pubDate>
<guid>http://snipplr.com/view/53010/bind-enum-to-combobox/</guid>
</item>
<item>
<title>(C#) Get enum values by Reflection - ronklein</title>
<link>http://snipplr.com/view/48028/get-enum-values-by-reflection/</link>
<description><![CDATA[ <p>This code keeps the code order. For instance:

public enum Color
{
    Red = 100,
    Green = 0,
    Blue = 5        
}


-Will generate the names as Red, Green, Blue.
Using the Enum.GetNames approach won't make it.</p> ]]></description>
<pubDate>Fri, 28 Jan 2011 02:50:31 GMT</pubDate>
<guid>http://snipplr.com/view/48028/get-enum-values-by-reflection/</guid>
</item>
<item>
<title>(C#) Convert string to enum - samwa</title>
<link>http://snipplr.com/view/46477/convert-string-to-enum/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Tue, 04 Jan 2011 21:21:45 GMT</pubDate>
<guid>http://snipplr.com/view/46477/convert-string-to-enum/</guid>
</item>
<item>
<title>(C#) Allow enums to have spaces - dotNetkow</title>
<link>http://snipplr.com/view/44388/allow-enums-to-have-spaces/</link>
<description><![CDATA[ <p>By default, enum values cannot have spaces in them (i.e. "Created Date" can only be "CreatedDate").  With this method, we can change that!</p> ]]></description>
<pubDate>Fri, 19 Nov 2010 02:55:51 GMT</pubDate>
<guid>http://snipplr.com/view/44388/allow-enums-to-have-spaces/</guid>
</item>
<item>
<title>(Java) Get enum from integer value (ordinal) - studioevoque</title>
<link>http://snipplr.com/view/43711/get-enum-from-integer-value-ordinal/</link>
<description><![CDATA[ <p>A simple way to get the enum indicated by its ordinal value.</p> ]]></description>
<pubDate>Mon, 08 Nov 2010 01:50:44 GMT</pubDate>
<guid>http://snipplr.com/view/43711/get-enum-from-integer-value-ordinal/</guid>
</item>
<item>
<title>(Java) The Planet enum example - studioevoque</title>
<link>http://snipplr.com/view/42422/the-planet-enum-example/</link>
<description><![CDATA[ <p>An example to make myself remember how powerful Java enums are!</p> ]]></description>
<pubDate>Sun, 17 Oct 2010 03:22:15 GMT</pubDate>
<guid>http://snipplr.com/view/42422/the-planet-enum-example/</guid>
</item>
<item>
<title>(C#) Get enum description - samdanae</title>
<link>http://snipplr.com/view/39337/get-enum-description/</link>
<description><![CDATA[ <p>Description attribute retrieval using an extension method.</p> ]]></description>
<pubDate>Fri, 20 Aug 2010 09:13:35 GMT</pubDate>
<guid>http://snipplr.com/view/39337/get-enum-description/</guid>
</item>
<item>
<title>(C#) Iterate through Enum - mkurdziel</title>
<link>http://snipplr.com/view/31070/iterate-through-enum/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Wed, 07 Apr 2010 15:12:06 GMT</pubDate>
<guid>http://snipplr.com/view/31070/iterate-through-enum/</guid>
</item>
<item>
<title>(C#) Enum Limitation Fix Using a Generic Class and Implicit Casting - bryanlyman</title>
<link>http://snipplr.com/view/24778/enum-limitation-fix-using-a-generic-class-and-implicit-casting/</link>
<description><![CDATA[ <p>For .net 2.0+   The one limitation to enumerations is revealed when you try to reverse lookup an enumeration value using Enum.Parse(). The parse function will return an inconsistent enum object if ever there are two or more enumerations with the same numeric value. This class fixes that problem. Written as a system extension and using implicit casting, the process has been made extremely easy and made the syntax for the parse function even simpler. The process even allows enumeration names starting with a number or the name of a C# keyword as long as the name is preceded by an underscore. The implicit cast from an Enum object to a Enum.Cast object has been deliberately left out to account for single directional assignment, which forces the class to be used properly. An Enum to Cast object lookup would defeat the whole purpose of the class if the implicit operator is used during runtime; for this purpose a user assignment operator of type String is supplied. This simply forces the user to use Cast = Enum.ToString() to parse to a correct object. The ToString() overload for a Cast object returns a Friendly name which replaces all underscores with spaces and even allows double underscores for commas and triple underscores for periods; for this reason, the implicit \"from string\" caster also converts from a friendly name to the proper Enum object. This makes it very handy for enumerating through a list of items for a combo or list box and converting back to the proper object by simply supplying the name of the list item.</p> ]]></description>
<pubDate>Mon, 14 Dec 2009 18:03:38 GMT</pubDate>
<guid>http://snipplr.com/view/24778/enum-limitation-fix-using-a-generic-class-and-implicit-casting/</guid>
</item>
<item>
<title>(C#) Binding an enum to a control (such as a ComboBox or DataGridView) with a custom string for each enum value - pckujawa</title>
<link>http://snipplr.com/view/23615/binding-an-enum-to-a-control-such-as-a-combobox-or-datagridview-with-a-custom-string-for-each-enum-value/</link>
<description><![CDATA[ <p>When you want to put the values of an enumeration into a ComboBox or similar control, there is no obvious or easy way to customize the text displayed. 

[UPDATE] The best method I've found is to use a custom TypeConverter. I haven't worked out all the intricacies, but the code below works for combo/listboxes and datagridviews, and I imagine that it would work with all IComponents.

One method below uses the System.ComponentModel.DescriptionAttribute to create a nice description of the enum value and then reads those values using reflection and an extension method (requires .NET 3.5 - see the references for alternatives). Once those values are read, they are put in a Dictionary in order to act as the DataSource for a ComboBox (named w_wifiSecurityIO in the example). 

Unfortunately, attributes (to my knowledge) cannot access resources, so the first method does not allow for localization. To overcome this challenge, I have used a Dictionary as the control's Tag and then use the SelectedItem.ToString as the lookup for the enum value selected (works with multiple selections and Flags enums as well, e.g. in a CheckedListBox). The second method uses a Dictionary for the DataSource, sets the DisplayMember and ValueMember properties, then retrieves using the SelectedValue property.

(Since the initialization code is only called once, and there are only a few enum values, the performance hit for using reflection is negligible. Even if it were noticeable, it might still be warranted when compared to other methods of customizing the displayed text.)

The third method comes from the answer to a [StackOverflow question about the subject](http://stackoverflow.com/questions/1415140/c-enums-can-my-enums-have-friendly-names/1415460#1415460) and uses a simple extension method that allows for localization.

Another method is [enumeration classes](http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/08/12/enumeration-classes.aspx).

(Used in RedBird::Hawk.)

References:
[msmvps.com](http://msmvps.com/blogs/deborahk/archive/2009/07/10/enum-binding-to-the-description-attribute.aspx)
[codeproject](http://www.codeproject.com/KB/cs/enumdatabinding.aspx)
[blogs.msdn](http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx)
[local ms help](ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system/html/7f153948-1284-313b-1f33-a6ed0d817322.htm) (DescriptionAttribute Class)</p> ]]></description>
<pubDate>Mon, 23 Nov 2009 14:52:51 GMT</pubDate>
<guid>http://snipplr.com/view/23615/binding-an-enum-to-a-control-such-as-a-combobox-or-datagridview-with-a-custom-string-for-each-enum-value/</guid>
</item>
<item>
<title>(Java) TimeZone Enum - fuzzylollipop</title>
<link>http://snipplr.com/view/23131/timezone-enum/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Sat, 14 Nov 2009 18:47:37 GMT</pubDate>
<guid>http://snipplr.com/view/23131/timezone-enum/</guid>
</item>
<item>
<title>(Java) TimeZone Enum - fuzzylollipop</title>
<link>http://snipplr.com/view/23038/timezone-enum/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Fri, 13 Nov 2009 02:01:24 GMT</pubDate>
<guid>http://snipplr.com/view/23038/timezone-enum/</guid>
</item>
<item>
<title>(Java) Enum serializaition and deserialization - jasonseney</title>
<link>http://snipplr.com/view/21108/enum-serializaition-and-deserialization/</link>
<description><![CDATA[ <p>//Sample use
    String dbVal = getSerializedForm(Suit.SPADE);

    // save dbVal to db in larger insert/update ...
    Suit suit = deserialize(Suit.class, dbVal);

*Credit: Dov Wasserman*</p> ]]></description>
<pubDate>Tue, 13 Oct 2009 14:35:58 GMT</pubDate>
<guid>http://snipplr.com/view/21108/enum-serializaition-and-deserialization/</guid>
</item>
<item>
<title>(C#) Create IEnumerable&amp;amp;lt;string&amp;amp;gt; to manually enumerate over string values - pckujawa</title>
<link>http://snipplr.com/view/11153/create-ienumerableltstringgt-to-manually-enumerate-over-string-values/</link>
<description><![CDATA[ <p>Ever need to enumerate over a collection of strings? Simple, just `foreach(string str in stringContainer)` and you've got them, right? Well, what if you want to enumerate over them manually? Basically, you can't (as far as I've found). You can do `stringContainer.GetEnumerator()`, but that returns a System.Collections.IEnumerator, which holds objects, not strings (thus requiring an explicit cast to get the string value). That is, you cannot do  
    `IEnumerator en = stringContainer.GetEnumerator();  
    en.MoveNext();  
    string myString = en.Current;`  
You have to do  
    `string myString = Convert.ToString(en.Current);`

I don't know why C# doesn't have this built in, but it doesn't (and trying to cast to IEnumerator&amp;lt;string&amp;gt; throws an exception). So, there are a lot of homegrown solutions out there, but this one is the easiest for my application. I needed to read in values from a file and assign them to string variables. Since there are a lot of variables, and their order is sometimes changed between versions, it is a pain to assign each variable by its array index (the array being created by reading in each line and splitting it with a comma). It is much easier to change the ordering of variables by simply moving the line up or down in the source code. Thus the need for an enumerator and my dilemma.

The URL refers to similar code from an O'Reilly book.

The 'yield return' statement essentially returns one value from a collection each time a value is needed (such as in a foreach or enum.MoveNext), but keeps track of ordering for you. Pretty slick. Google it for more info.</p> ]]></description>
<pubDate>Tue, 13 Jan 2009 19:04:35 GMT</pubDate>
<guid>http://snipplr.com/view/11153/create-ienumerableltstringgt-to-manually-enumerate-over-string-values/</guid>
</item>
<item>
<title>(SQL) mysql php get enum values - ginoplusio</title>
<link>http://snipplr.com/view/10429/mysql-php-get-enum-values/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Fri, 12 Dec 2008 09:28:03 GMT</pubDate>
<guid>http://snipplr.com/view/10429/mysql-php-get-enum-values/</guid>
</item>
<item>
<title>(PHP) Retrieve valid entries for a MySQL ENUM/SET column - Wardy</title>
<link>http://snipplr.com/view/10428/retrieve-valid-entries-for-a-mysql-enumset-column/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Fri, 12 Dec 2008 06:50:48 GMT</pubDate>
<guid>http://snipplr.com/view/10428/retrieve-valid-entries-for-a-mysql-enumset-column/</guid>
</item>
</channel>
</rss>