﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Enumeration</title>
	<atom:link href="http://byatool.com/tag/enumeration/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is where love and programming met on a drunken night and the rest is history.</description>
	<lastBuildDate>Fri, 10 Sep 2010 18:53:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Convert Enum to Dictionary: Another Silly Method</title>
		<link>http://byatool.com/utilities/another-silly-method-just-for-you/</link>
		<comments>http://byatool.com/utilities/another-silly-method-just-for-you/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 18:22:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Enumeration]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/14/another-silly-method-just-for-you/</guid>
		<description><![CDATA[So what if you want the names and values from an Enum, but wanted them in dictionary form. Well shoot, a little bit of linq and little bit of that and you got this: public static IDictionary&#60;String, Int32&#62; ConvertEnumToDictionary&#60;K&#62;() { if (typeof(K).BaseType != typeof(Enum)) { throw new InvalidCastException(); } return Enum.GetValues(typeof(K)).Cast&#60;Int32&#62;().ToDictionary(currentItem =&#62; Enum.GetName(typeof(K), currentItem)); } [...]]]></description>
			<content:encoded><![CDATA[<p>So what if you want the names and values from an Enum, but wanted them in dictionary form. Well shoot, a little bit of linq and little bit of that and you got this:</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">IDictionary</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Int32</span>&gt; ConvertEnumToDictionary&lt;K&gt;()
{
 <span style="color: #3333ff;">if</span> (<span style="color: #3333ff;">typeof</span>(K).BaseType != <span style="color: #3333ff;">typeof</span>(<span style="color: #00cccc;">Enum</span>))
 {
   <span style="color: #3333ff;">throw</span> <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">InvalidCastException</span>();
 }

 <span style="color: #3333ff;">return</span> <span style="color: #00cccc;">Enum</span>.GetValues(<span style="color: #3333ff;">typeof</span>(K)).Cast&lt;<span style="color: #00cccc;">Int32</span>&gt;().ToDictionary(currentItem =&gt; <span style="color: #00cccc;">Enum</span>.GetName(<span style="color: #3333ff;">typeof</span>(K), currentItem));
}</pre>
<p>As you might see, pretty simple.</p>
<p>Ok so ToDictionary is kind of odd looking maybe, but really isn&#8217;t that big of a deal. Basically it assumes the items in the list are the value, but you need to tell it what the key is. In this case, the int values for the enum are the value for the dictionary where the name that matches said int value will become the key for the dictionary.</p>
<p>So basically, get the values for the enum. Turn that into an IEnumerable list of Int32, create a Dictionary from that.</p>
<p>USINGS!!111</p>
<pre> <span style="color: #3333ff;">using</span> System;
 <span style="color: #3333ff;">using</span> System.Collections.Generic;
 <span style="color: #3333ff;">using</span> System.Linq;</pre>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/" title="Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH">Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH</a></li><li><a href="http://byatool.com/general-coding/uhg-it-wont-end/" title="Uhg It Won&#8217;t End">Uhg It Won&#8217;t End</a></li><li><a href="http://byatool.com/pontification/what-is-readable-addon/" title="What Is Readable Addon">What Is Readable Addon</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/another-silly-method-just-for-you/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Random Enumeration Generator with Generics</title>
		<link>http://byatool.com/general-coding/random-enumeration-generator-with-generics/</link>
		<comments>http://byatool.com/general-coding/random-enumeration-generator-with-generics/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 21:04:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Enumeration]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/06/25/random-enumeration-generator-with-generics/</guid>
		<description><![CDATA[public static I RandomEnumeration&#60;I&#62;() { I enumerationToCheck; Int32 indexToUse; String[] names; //Use activator to create an instance of the type I enumerationToCheck = System.Activator.CreateInstance&#60;I&#62;(); //Make sure the instance is an Enumeration //Unfortunately you can't check that in the method //delcaring using "which". if (enumerationToCheck as Enum == null) { throw new InvalidOperationException(); } //Get the [...]]]></description>
			<content:encoded><![CDATA[<pre><strong>
public static I RandomEnumeration&lt;I&gt;()
{
  I enumerationToCheck;
  Int32 indexToUse;
  String[] names;

  <span style="color: #009900;">//Use activator to create an instance of the type I</span>
  enumerationToCheck = System.Activator.CreateInstance&lt;I&gt;();

  <span style="color: #009900;">//Make sure the instance is an Enumeration</span>
<span style="color: #009900;">   //Unfortunately you can't check that in the method </span>
<span style="color: #009900;">   //delcaring using "which".</span>
  if (enumerationToCheck as Enum == null)
  {
    throw new InvalidOperationException();
  }

  <span style="color: #009900;">//Get the list of the enumeration item names</span>
  names = Enum.GetNames(typeof(I));

  if (names.Length &gt; 0)
  {
    <span style="color: #009900;">//Grab a random name within the boundaries of the</span>
<span style="color: #009900;">     //names collection.</span>
    indexToUse = RandomInt32(0, names.Length);
    <span style="color: #009900;">//parse the name to create the random enum</span>
    enumerationToCheck = (I)Enum.Parse(typeof(I), names[indexToUse]);
  }

  return enumerationToCheck;

}
</strong></pre>
<p>Usage:</p>
<pre><strong>
  SomeEnum test = RandomEnumeration();
</strong></pre>
<p>Why bother? For unit testing and creating test classes. Possibly<br />
for defaults on an enumeration, but not really needed since<br />
they are value types. Oh yeah AND BECAUSE I FELT LIKE IT. I don&#8217;t<br />
have to explain myself to you.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/utilities/random-string-of-specified-length-with-enumerable/" title="Random String of Specified Length with Enumerable">Random String of Specified Length with Enumerable</a></li><li><a href="http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/" title="Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH">Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH</a></li><li><a href="http://byatool.com/general-coding/the-switch-remover/" title="The Switch Remover: Convert Switch Statements to Dictionaries">The Switch Remover: Convert Switch Statements to Dictionaries</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/random-enumeration-generator-with-generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
