Iterating enum names and values in C#
Have you ever wanted to create a SELECT element with the names and values of an enum Type? I'll readily admit, in my earlier programming days I used to create the control manually.
Thanks to the C# System.Enum class in .NET and its static helper methods, I can enumerate the Names and Values in an enum Type, increasing maintainability of my application.
System.Enum contains two methods that are of particular interest, these are Enum.GetValues and Enum.GetNames. I'll show you simple examples as to their usage.
In this example, we'll use the following enum declaration:
In the first example, I'll enumerate the values. Enumerators, by default start with 0. This of course can be overridden in the declaration but is beyond the scope of this article.
foreach (int value in Enum.GetValues(typeof(ProductType)))
{
// Do Something with the Values
}
In the next example, I'll enumerate the names of each enum option. This may be useful for generating a SELECT element with options based off of your enum Type.
foreach (string name in Enum.GetNames(typeof(ProductType)))
{
// Do Something with the Names
}
Finally, if you want to create Name,Value pairs, you can do something like the following:
For more information on the System.Enum class, refer to the MSDN System.Enum members page, http://msdn.microsoft.com/en-us/library/system.enum_members.aspx
---
As an advanced implementation, the Enum.GetNames method was used to serialize the enum's names as a SELECT element. The technique uses XSLT to transform an object's XML serialization into XHTML.
Below is the code that creates a "product editor" by transforming a product's XML using XSLT.
Product product = new Product();
// Generate the paratmeters for the transform
System.Xml.Xsl.XsltArgumentList args = new XsltArgumentList();
args.AddParam(
"ProductTypes",
"",
Albatross.Xml.Utility.XmlSerialize(
Enum.GetNames(typeof(ProductType))));
// Create the editor xhtml for the new product
string xhtml = Albatross.Xml.Utility.TransformObject(
product,
/templates/product-editor.xsl",
args)));
Here is a stripped version of the XSL template used to generate the product editor. ProductType names are serialized into an XML ArrayOfString and are accessed by iterating through $ProductTypes/ArrayOfString/string.
<xsl:param name="ProductTypes"></xsl:param>
<xsl:template name="SelectProductType">
<select name="producttype">
<xsl:for-each select="$ProductTypes/ArrayOfString/string">
<option value="{current()}">
<xsl:value-of select="current()"/>
</option>
</xsl:for-each>
</select>
</xsl:template>
</xsl:stylesheet
Recent blog posts
- Quick Memory Improvement for Firefox
- Canned VirtualPC Instances for IE 6, 7, 8 on XP/Vista
- Checking assembly dependencies for .NET
- Google's Public DNS
- Server Utility Functions for Non-Web Apps
- reCAPTCHA for ASP.NET MVC that uses ModelState
- Adding a container to ValidationSummary helper in ASP.NET MVC
- Generic XML Serialization Class
- Re-throwing Exceptions in C# with InternalPreserveStackTrace
- Solving xsd generation error: 'The element .... is missing'
Recent comments
7 weeks 4 days ago
24 weeks 1 day ago
27 weeks 2 days ago
30 weeks 3 days ago
36 weeks 1 day ago
46 weeks 14 hours ago
1 year 30 weeks ago
1 year 47 weeks ago
1 year 47 weeks ago
2 years 2 weeks ago