<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: More reasons never to use C# enums</title>
	<atom:link href="http://skizz.biz/blog/2004/01/20/more-reasons-never-to-use-c-enums/feed/" rel="self" type="application/rss+xml" />
	<link>http://skizz.biz/blog/2004/01/20/more-reasons-never-to-use-c-enums/</link>
	<description>Chris Stevenson's blog</description>
	<pubDate>Wed, 10 Mar 2010 09:59:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Redsplinter</title>
		<link>http://skizz.biz/blog/2004/01/20/more-reasons-never-to-use-c-enums/#comment-1652</link>
		<dc:creator>Redsplinter</dc:creator>
		<pubDate>Wed, 17 Jun 2009 19:00:03 +0000</pubDate>
		<guid isPermaLink="false">http://skizz.biz/blog/2004/01/20/more-reasons-never-to-use-c-enums/#comment-1652</guid>
		<description>bump
soapbox
AHA, anon posting!  Somewhere I can moan about Microsoft's bad design choices without getting spam.

@Chris: Beer is the answer ;)
@Michael: Excellent link.  I try to steer away from the out keyword though.  Old nightmares from expressions like "void** foo = var-&#62;warg(void*, &#38;args, count)".  Psychological block I'm trying to work through &#62;.&#60;
@stEvil: Really now?  Fisher price?

Enums are one of my main beefs with C#.  To have such a strongly typed language allow casting willy nilly as above is both blasphemic and hypocritical.
I don't understand why it would have killed them to have the equivalent of:

"if( Enum.IsDefined(typeof(MyEnum), value) ) {
	return (MyEnum)Enum.ToObject(typeof(MyEnum), value);
}
throw new InvalidCastException("Destination type contains no definition for value " + value);"

_somewhere_ in their cast.


I'm creating a toolkit that has many configurable values that are dropped in a combo and/or are configured in text by the user, as they prefer.  For the convenience of all parties, the best way to do this is aliasing these values in an enum that can convert from a textual description or numeric value in a type safe way.

At least extension methods make it a bit less painful:
public static T toEnum(this long/int/short source)...
public static Enum toEnum( this Type source, long/int/short value )...
public static Enum toEnumFromDesc( this Type source, Predicate predicate )...
public static T toEnumFromDesc(this string source)...

(I didn't say less clunky, just less painful.  And yes the guts of those get kinda strange and use custom attributes.)

I mean seriously, the enums along with unchecked exceptions, no fallthrough switches, goto, etc. make me want to start a Java mutiny.

pine
SampleRate rate = (SampleRate)666; //throws error
/pine
/soapbox
/bump</description>
		<content:encoded><![CDATA[<p>bump<br />
soapbox<br />
AHA, anon posting!  Somewhere I can moan about Microsoft&#8217;s bad design choices without getting spam.</p>
<p>@Chris: Beer is the answer ;)<br />
@Michael: Excellent link.  I try to steer away from the out keyword though.  Old nightmares from expressions like &#8220;void** foo = var-&gt;warg(void*, &amp;args, count)&#8221;.  Psychological block I&#8217;m trying to work through &gt;.&lt;<br />
@stEvil: Really now?  Fisher price?</p>
<p>Enums are one of my main beefs with C#.  To have such a strongly typed language allow casting willy nilly as above is both blasphemic and hypocritical.<br />
I don&#8217;t understand why it would have killed them to have the equivalent of:</p>
<p>&#8220;if( Enum.IsDefined(typeof(MyEnum), value) ) {<br />
	return (MyEnum)Enum.ToObject(typeof(MyEnum), value);<br />
}<br />
throw new InvalidCastException(&#8221;Destination type contains no definition for value &#8221; + value);&#8221;</p>
<p>_somewhere_ in their cast.</p>
<p>I&#8217;m creating a toolkit that has many configurable values that are dropped in a combo and/or are configured in text by the user, as they prefer.  For the convenience of all parties, the best way to do this is aliasing these values in an enum that can convert from a textual description or numeric value in a type safe way.</p>
<p>At least extension methods make it a bit less painful:<br />
public static T toEnum(this long/int/short source)&#8230;<br />
public static Enum toEnum( this Type source, long/int/short value )&#8230;<br />
public static Enum toEnumFromDesc( this Type source, Predicate predicate )&#8230;<br />
public static T toEnumFromDesc(this string source)&#8230;</p>
<p>(I didn&#8217;t say less clunky, just less painful.  And yes the guts of those get kinda strange and use custom attributes.)</p>
<p>I mean seriously, the enums along with unchecked exceptions, no fallthrough switches, goto, etc. make me want to start a Java mutiny.</p>
<p>pine<br />
SampleRate rate = (SampleRate)666; //throws error<br />
/pine<br />
/soapbox<br />
/bump</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stEvil</title>
		<link>http://skizz.biz/blog/2004/01/20/more-reasons-never-to-use-c-enums/#comment-1313</link>
		<dc:creator>stEvil</dc:creator>
		<pubDate>Thu, 04 Sep 2008 12:09:09 +0000</pubDate>
		<guid isPermaLink="false">http://skizz.biz/blog/2004/01/20/more-reasons-never-to-use-c-enums/#comment-1313</guid>
		<description>1. Thats why you use the enums values names "ie one, two" not explicit numeric values.

2. Operations using enums should use if or switch statements. When using a switch statement use a default case, and use it to either provide a default value, or throw an exception.

3. If the value MUST be within a range, make the enum private, instantiate it in a class, and provide a property with limiting values.

Enums aren't a problem, hacks writing "fisher-price" code are</description>
		<content:encoded><![CDATA[<p>1. Thats why you use the enums values names &#8220;ie one, two&#8221; not explicit numeric values.</p>
<p>2. Operations using enums should use if or switch statements. When using a switch statement use a default case, and use it to either provide a default value, or throw an exception.</p>
<p>3. If the value MUST be within a range, make the enum private, instantiate it in a class, and provide a property with limiting values.</p>
<p>Enums aren&#8217;t a problem, hacks writing &#8220;fisher-price&#8221; code are</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Goldobin</title>
		<link>http://skizz.biz/blog/2004/01/20/more-reasons-never-to-use-c-enums/#comment-935</link>
		<dc:creator>Michael Goldobin</dc:creator>
		<pubDate>Wed, 23 Apr 2008 14:08:28 +0000</pubDate>
		<guid isPermaLink="false">http://skizz.biz/blog/2004/01/20/more-reasons-never-to-use-c-enums/#comment-935</guid>
		<description>With the C# 3.0 it is possible to extend it with TryParse and do it nicer than Microsoft :) http://lazyloading.blogspot.com/2008/04/enumtryparse-with-net-35-extension.html</description>
		<content:encoded><![CDATA[<p>With the C# 3.0 it is possible to extend it with TryParse and do it nicer than Microsoft :) <a href="http://lazyloading.blogspot.com/2008/04/enumtryparse-with-net-35-extension.html" rel="nofollow">http://lazyloading.blogspot.com/2008/04/enumtryparse-with-net-35-extension.html</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
