<?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>Cameron McKay &#187; enum</title>
	<atom:link href="http://cdmckay.org/blog/tag/enum/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming, Politics and Game Design</description>
	<lastBuildDate>Thu, 09 Sep 2010 06:34:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Inconsistencies in the .NET Enum class</title>
		<link>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/</link>
		<comments>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 05:59:44 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Dnum]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[enum]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=948</guid>
		<description><![CDATA[During a recent project of mine, I had to do a lot of enum manipulation using the .NET Enum class and I have to say I wasn&#8217;t impressed.  Besides the shortcoming of it not being a generic class (and thus not being especially type-safe), Enum also has some strange inconsistencies in terms of how [...]]]></description>
			<content:encoded><![CDATA[<p>During a recent project of mine, I had to do a lot of enum manipulation using the .NET Enum class and I have to say I wasn&#8217;t impressed.  Besides the shortcoming of it not being a generic class (and thus not being especially type-safe), Enum also has some strange inconsistencies in terms of how it handles matched integral types.</p>
<p><span id="more-948"></span></p>
<h3>GetName</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx">GetName</a></code> return null for any integral type you pass it except those that correspond to a constant?</p>
<p>For example, this is completely valid:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #FF0000;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #FF0000;">byte</span> <span style="color: #000000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
var fruit <span style="color: #008000;">=</span> <span style="color: #FF0000;">Enum</span>.<span style="color: #0000FF;">GetName</span><span style="color: #000000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Fruit<span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">ulong</span>.<span style="color: #0000FF;">MaxValue</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// fruit now equals null</span></pre></div></div>

<p>In every other case in the Enum class where you pass an integral value outside the range of the underlying type, you get an exception.  Except here.</p>
<h3>IsDefined</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx">IsDefined</a></code> throw an exception when you pass it an integral value that can be safely narrowed to the enumeration&#8217;s underlying type?</p>
<p>For example, this will throw an exception:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #FF0000;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #FF0000;">byte</span> <span style="color: #000000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
var defined <span style="color: #008000;">=</span> <span style="color: #FF0000;">Enum</span>.<span style="color: #0000FF;">IsDefined</span><span style="color: #000000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Fruit<span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Why?  Because it&#8217;s a integer literal (and therefore type <code>int</code>) and the underlying type of <code>Fruit</code> is <code>byte</code>.  This would be alright if this is how the Enum class consistently treated this case, but it&#8217;s not.  In every other method, the Enum class is OK with you passing in an <code>int</code> value for a <code>byte</code> enumeration (as long as it&#8217;s within the range of the underlying type).</p>
<h3>ToObject</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.toobject.aspx">ToObject</a></code> silently overflow for values outside the range of the enumeration&#8217;s underlying type?</p>
<p>For example, this code will give the following non-intuitive result:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #FF0000;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #FF0000;">byte</span> <span style="color: #000000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
var apple <span style="color: #008000;">=</span> <span style="color: #FF0000;">Enum</span>.<span style="color: #0000FF;">ToObject</span><span style="color: #000000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Fruit<span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var elppa <span style="color: #008000;">=</span> <span style="color: #FF0000;">Enum</span>.<span style="color: #0000FF;">ToObject</span><span style="color: #000000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Fruit<span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">256</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>apple, elppa<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Assuming that we ignore the fact that silently overflowing is typically a bad idea, this is again completely inconsistent with the rest of the library.  Why overflow here, but not in <code>GetName</code>?</p>
<h3>Conclusion</h3>
<p>The .NET Enum class is not lacking in it&#8217;s ability to extract any information you need from an enumeration.  It is, however, a little inconsistent in terms of how it treats underlying type mismatches, overflows and narrowing conversions.  Barring a new, generified Enum class from Microsoft, the best way to deal with these inconsistencies is to either be aware of them, or use a third-party library to hide them.</p>
<p><em>(Edit: Some people have noted that my conclusion was a bit weak and preachy towards Dnum, which I have to admit is not without truth.  I&#8217;ve seen edited it.  For the record, the old conclusion was: &#8220;Fortunately, these and other problems have been fixed in my <a href="http://cdmckay.org/blog/dnum/">Dnum</a> library, which should be released in the next couple days.  Be sure to check it out.&#8221;).</em></p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to put spaces in your NHibernate enums</title>
		<link>http://cdmckay.org/blog/2009/07/12/how-to-put-spaces-in-your-nhibernate-enums/</link>
		<comments>http://cdmckay.org/blog/2009/07/12/how-to-put-spaces-in-your-nhibernate-enums/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 02:33:25 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[enum]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=770</guid>
		<description><![CDATA[NHibernate lets you map C# enums to database columns.  This can be quite convenient in cases where you have a database column for something like gender.  To map them, you follow the procedure outlined here.
That works all fine and dandy, as long as the strings you want to store in the database are [...]]]></description>
			<content:encoded><![CDATA[<p>NHibernate lets you map C# enums to database columns.  This can be quite convenient in cases where you have a database column for something like gender.  To map them, you follow the procedure outlined <a href="http://codebetter.com/blogs/jeremy.miller/archive/2006/02/20/138732.aspx">here</a>.</p>
<p>That works all fine and dandy, as long as the strings you want to store in the database are valid C# identifiers.</p>
<p><span id="more-770"></span></p>
<p>What do I mean?  Well, consider an enum of something like shipping countries.  Imagine we&#8217;re only shipping to Canada and the USA.  That&#8217;s easy enough:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">// Country.cs</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> Country
<span style="color: #000000;">&#123;</span>
  Canada,
  USA
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now imagine that after we write that code, we find out the customer hates <a href="http://en.wikipedia.org/wiki/Three-letter_acronym">TLA</a>s and wants the database entry to contain &#8220;United States of America&#8221;.  When we go to the enum to change it, we realize we&#8217;re screwed.  You can&#8217;t have spaces in a C# identifier!</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">// Country.cs</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> Country
<span style="color: #000000;">&#123;</span>
  Canada,
  United States of America <span style="color: #008080; font-style: italic;">// Compile error!</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Fortunately, the ever-flexible NHibernate can handle this case using the procedure outlined <a href="http://softwareblog.morlok.net/2009/07/02/mapping-enums-to-custom-strings-in-nhibernate/">here</a>.</p>
<p>Let&#8217;s apply this procedure to the shipping locations example.  Recall that C# was not happy with us putting spaces in our enum members.  How can we make that code compile?  Well, an underscore is a valid part of a C# identifier, and it&#8217;s kinda like a space, so let&#8217;s try that:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">// Country.cs</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> Country
<span style="color: #000000;">&#123;</span>
  Canada,
  United_States_of_America <span style="color: #008080; font-style: italic;">// C# is happy!</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Next, we need to derive our <code>EnumStringType</code> class so that NHibernate will store the string value of the enum instead of the integer value:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">// CountryEnumStringType.cs</span>
<span style="color: #FF0000;">class</span> CountryEnumStringType <span style="color: #008000;">:</span> NHibernate.<span style="color: #0000FF;">Type</span>.<span style="color: #0000FF;">EnumStringType</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// The 30 is the size of the field in the DB.</span>
  <span style="color: #0600FF;">public</span> CountryEnumStringType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
      <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>Country<span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">30</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
  <span style="color: #000000;">&#125;</span>
  ...
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Cool, cool, so far so good.  This is about how much we&#8217;d need to do if we had no spaces to worry about.  Unfortunately, if we used these two classes we made as is, the database would contain &#8220;United_States_of_America&#8221; instead of &#8220;United States of America&#8221;.  And that&#8217;s no good&#8230;  so how do we get around that?</p>
<p>Why, we need to override the <code>GetValue</code> and <code>GetInstance</code> methods, of course!</p>
<p>Let&#8217;s <a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/">plunk open the NHibernate source code</a> and see what they have there now, to use as a guide.  Ahh, <a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Type/EnumStringType.cs">there she is</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
&nbsp;
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">object</span> GetInstance<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> code<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">//code is an named constants defined for the enumeration.</span>
  <span style="color: #0600FF;">try</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> StringToObject<span style="color: #000000;">&#40;</span>code <span style="color: #0600FF;">as</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
  <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ArgumentException ae<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> HibernateException<span style="color: #000000;">&#40;</span>
      <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Can't Parse {0} as {1}&quot;</span>,
      code, ReturnedClass.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>, ae<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
&nbsp;
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">object</span> GetValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> code<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">//code is an enum instance.</span>
  <span style="color: #0600FF;">return</span> code <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">?</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span> <span style="color: #008000;">:</span> code.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The comments aren&#8217;t the greatest, but we can figure out what&#8217;s going on.  <code>GetInstance</code> converts a string in an enum instance, and <code>GetValue</code> converts an enum to a string.</p>
<p>With that in mind, let&#8217;s get to work.  First, let&#8217;s put in some stubs in our <code>CountryEnumStringType</code> class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">// CountryEnumStringType.cs</span>
<span style="color: #FF0000;">class</span> CountryEnumStringType <span style="color: #008000;">:</span> NHibernate.<span style="color: #0000FF;">Type</span>.<span style="color: #0000FF;">EnumStringType</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// The 30 is the size of the field in the DB.</span>
  <span style="color: #0600FF;">public</span> CountryEnumStringType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
      <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>Location<span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">30</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">///</span>
  <span style="color: #008080; font-style: italic;">/// Converts a string to an enum instance.</span>
  <span style="color: #008080; font-style: italic;">///</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetInstance<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> code<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
&nbsp;
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">///</span>
  <span style="color: #008080; font-style: italic;">/// Converts a enum instance to a string.</span>
  <span style="color: #008080; font-style: italic;">///</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> code<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
&nbsp;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Sweet.  Now, our basic strategy here is this:</p>
<ul>
<li>when NHibernate asks for an enum instance (and passes a string), we want to replace spaces in the string with underscores, and then find the corresponding enum instance.</li>
<li>when NHibernate asks for a string (and passes an enum instance), we want to convert the enum to a string, and then replace all underscores with spaces.</li>
</ul>
<p>First, we modify <code>GetInstance</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// Converts a string to an enum instance.</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetInstance<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> code<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// This is the string coming from the database, so it will</span>
  <span style="color: #008080; font-style: italic;">// have spaces in it that we need to turn into underscores.</span>
  var str <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span> code<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// If the string is null, just pass it on, no need to replace</span>
  <span style="color: #008080; font-style: italic;">// spaces with underscores.</span>
  <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> StringToObject<span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">return</span> StringToObject<span style="color: #000000;">&#40;</span>str.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, <span style="color: #666666;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In case you&#8217;re wondering where the hell <code>StringToObject</code> came from, and what it does, I can tell you this:</p>
<ul>
<li>It was used in the code for the <code>EnumStringType</code> class,</li>
<li>its name implies that it&#8217;s used to convert a string into an object,</li>
<li>the context in which it is used supports the previous implication, and</li>
<li>if you really want to know what it does, read the code for the <code>AbstractEnumType</code> <a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Type/AbstractEnumType.cs">base class</a>.</li>
</ul>
<p>With that out of the way, let&#8217;s move on to writing the code for the <code>GetValue</code> method.  In this case we&#8217;re looking at converting an enum instance into a string (that will eventually be stored in the database):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// Converts a enum instance to a string.</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> code<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">return</span> code <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span>
    <span style="color: #008000;">?</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>
    <span style="color: #008000;">:</span> code.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'_'</span>, <span style="color: #666666;">' '</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The <code>GetValue</code> is pretty straight forward.  We basically just tagged on the <code>Replace</code> method to replace all underscores with spaces.</p>
<p>That should about do it.  To close, I&#8217;ll give a sample usage of it:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">// Order.cs</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Order
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">long</span> Id <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  ...
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> Country Country <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  ...
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Then this would be the mapping:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:Consolas, monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- Order.hbm.xml --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Order&quot;</span> <span style="color: #000066;">table</span>=<span style="color: #ff0000;">&quot;TblOrder&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Country&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;WebApp.Domain.CountryEnumStringType, WebApp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/07/12/how-to-put-spaces-in-your-nhibernate-enums/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
