<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx</link><description>We've been talking about enums a bit, in one of those cases where I'm looking into something and I then get a request about that same thing from some other source. Or multiple sources, as it happened this time. Yesterday, I was working on my TechEd presentation</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129397</link><pubDate>Mon, 10 May 2004 23:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129397</guid><dc:creator>jaybaz [MS]</dc:creator><description>&amp;quot;enums are not sets&amp;quot;&lt;br&gt;&lt;br&gt;Enums aren't very OO, either.  If you're worried about your enum getting used incorectly, force correct usage with OO programming techniques.&lt;br&gt;&lt;br&gt;For example, check out &lt;br&gt;   &lt;a target="_new" href="http://www.refactoring.com/catalog/replaceTypeCodeWithClass.html"&gt;http://www.refactoring.com/catalog/replaceTypeCodeWithClass.html&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://www.refactoring.com/catalog/replaceTypeCodeWithStateStrategy.html"&gt;http://www.refactoring.com/catalog/replaceTypeCodeWithStateStrategy.html&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://www.refactoring.com/catalog/replaceTypeCodeWithSubclasses.html"&gt;http://www.refactoring.com/catalog/replaceTypeCodeWithSubclasses.html&lt;/a&gt;&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129426</link><pubDate>Mon, 10 May 2004 23:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129426</guid><dc:creator>ron</dc:creator><description>How can I use hyphens in enums? The following fails:&lt;br&gt;&lt;br&gt;public enum CssTypes&lt;br&gt;	{&lt;br&gt;		background,&lt;br&gt;		background-color,&lt;br&gt;		background-image&lt;br&gt;	}&lt;br&gt;&lt;br&gt;&lt;br&gt;thanks,&lt;br&gt;-ron</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129434</link><pubDate>Tue, 11 May 2004 00:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129434</guid><dc:creator>Tomas Restrepo</dc:creator><description>Eric,&lt;br&gt;&lt;br&gt;I'd actually change the default case in the switch to throw an exception (InvalidOperationException?), instead of just failing silently. That would make it much more easier to figure out what's going on...</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129486</link><pubDate>Tue, 11 May 2004 02:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129486</guid><dc:creator>David M. Kean</dc:creator><description>or an InvalidEnumArgumentException...</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129552</link><pubDate>Tue, 11 May 2004 04:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129552</guid><dc:creator>Robin Debreuil</dc:creator><description>One thing I find myself doing a lot with enums is having a default XX.Empty tag first (that acts as kind of a default 'null' value). Recently I've also been using 'End' tags too. The advantage is you can very quickly validate a tag (assuming you aren't using custom values) with a compare, like&lt;br&gt;&lt;br&gt;Tag t = Tag.Open;&lt;br&gt;if(t &amp;gt; Tag.Empty &amp;amp;&amp;amp; t &amp;lt; Tag.End){ // valid }&lt;br&gt;&lt;br&gt;The main reason I went with this is I was having problems when inserting new values into the enum during development, it became a pain to hunt simple validation stuff down. When going to a new version, you can just add enums, and test for 'Tag.End2'. &lt;br&gt;&lt;br&gt;Not sure if this is the best way though - it has yet to blow up in my face at least. I don't always like the idea of a value that is just used as a marker, it really should be a 'silent' value. I guess you could use .GetNames.Count or something code heavy like that. Of course switching to defined values would be painful...&lt;br&gt;&lt;br&gt;I also usually throw an exception in the default switch clause (until release anyway!) when switching over an enum - just makes it easier to catch where the need to  make a change after inserting a new value into an enum. &lt;br&gt;&lt;br&gt;It would be great if the switch statement could be made to (optionally) throw an error when switching an enum and every case isn't explicitly covered.</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129581</link><pubDate>Tue, 11 May 2004 06:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129581</guid><dc:creator>JosephCooney</dc:creator><description>I wrote an small entry on this a while ago too....&lt;br&gt;&lt;a target="_new" href="http://dotnetjunkies.com/WebLog/josephcooney/archive/2004/04/08/11002.aspx"&gt;http://dotnetjunkies.com/WebLog/josephcooney/archive/2004/04/08/11002.aspx&lt;/a&gt;&lt;br&gt;</description></item><item><title>Eric Gunnerson discusses Enums, Validation and Versioning...</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129603</link><pubDate>Tue, 11 May 2004 10:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129603</guid><dc:creator>Code/Tea/Etc...</dc:creator><description /></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129647</link><pubDate>Tue, 11 May 2004 08:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129647</guid><dc:creator>Todd Girvin</dc:creator><description>Eric, before giving your TechEd presentation, you might want to remove the extra commas in your enumerations.  ;)</description></item><item><title>Great site</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129652</link><pubDate>Tue, 11 May 2004 12:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129652</guid><dc:creator>theDoctor .NET</dc:creator><description /></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129738</link><pubDate>Tue, 11 May 2004 13:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129738</guid><dc:creator>Jonathan Pryor</dc:creator><description>How do you use hyphens in enumeration values?  You don't.  The hyphen is not a valid character for identifiers.&lt;br&gt;&lt;br&gt;The problem is that hypens, or anything &amp;quot;hyphen-like&amp;quot;, such as non-breaking hyphen \u2011, figure dash \u2012, en dash \u2013, and minus sign \u2212, are all categorized as Pd (Punctuation, Dash).&lt;br&gt;&lt;br&gt;Identifiers, in turn, can only contain characters from the Unicode categories Lu (Letter, uppercase), Ll (Letter, lowercase), Lt (Letter, Titlecase), Lm (Letter, Modifier), Lo (Letter, other), Mn (Mark, non-spacing), Me (Mark, enclosing), Pc (Punctuation, connecting), Cf (Other, format), Nd (Number, decimal digit), and Nl (Number, letter).  Not necessarily in that order (numbers can't be the first character of an identifier).&lt;br&gt;&lt;br&gt;The above is basically an i18n-aware version of the &amp;quot;standard&amp;quot; regex-like [_\w][_\d\w]+ pattern for identifiers.&lt;br&gt;&lt;br&gt;In an ideal world, using @IDENTIFIER notation would save you, as prefixing an identifier with @ enables literal quoting, so you can have @bool to refer to a variable named 'bool'.  However, the standard tokenizing rules also apply, so @one-two breaks down to the tokens {'one', '-', 'two'}, which doesn't work for you.  If @IDENTIFIER included paranthesis for wrapping then you'd be fine saying @(one-two), allowing us to get all LISP-ish with our naming, but that wasn't done.&lt;br&gt;&lt;br&gt; - Jon&lt;br&gt;</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#129788</link><pubDate>Tue, 11 May 2004 14:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129788</guid><dc:creator>nfactorial</dc:creator><description>Todd, I see no extra commas in Eric's enumerations.&lt;br&gt;&lt;br&gt;The trailing comma on the final entry is allowed and is also how I write my enumerations. Saving you having to add it when you want to add a new value!&lt;br&gt;&lt;br&gt;Unless you meant something else, then I'm not sure what you mean :)&lt;br&gt;&lt;br&gt;n!</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130083</link><pubDate>Tue, 11 May 2004 21:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130083</guid><dc:creator>Jay Cook</dc:creator><description>I would try the following, use System.Enum.GetValues and then search the resulting array for a match.  This will eliminate the hardcoded switch and if statements.  For example,&lt;br&gt;&lt;br&gt;using System;&lt;br&gt;&lt;br&gt;namespace EnumExample&lt;br&gt;{&lt;br&gt;	enum Operation&lt;br&gt;	{&lt;br&gt;		Fold,&lt;br&gt;		Spindle,&lt;br&gt;		Mutilate,&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	class Class1&lt;br&gt;	{&lt;br&gt;		static void Foo(Operation op)&lt;br&gt;		{&lt;br&gt;			int value = (int) op;&lt;br&gt;			foreach(int i in Enum.GetValues(typeof(Operation)))&lt;br&gt;			{&lt;br&gt;				if (i == value)&lt;br&gt;				{&lt;br&gt;					Console.WriteLine(op.ToString() + &amp;quot; is a member of the enum&amp;quot;);&lt;br&gt;					return;&lt;br&gt;				}&lt;br&gt;			}&lt;br&gt;&lt;br&gt;			// not a member&lt;br&gt;			Console.WriteLine(op.ToString() + &amp;quot; is not a member of the enum&amp;quot;);&lt;br&gt;		}&lt;br&gt;&lt;br&gt;		static void Main()&lt;br&gt;		{&lt;br&gt;			Foo(Operation.Fold);&lt;br&gt;			Foo(Operation.Spindle);&lt;br&gt;			Foo(Operation.Mutilate);&lt;br&gt;			Foo((Operation) 42);&lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;</description></item><item><title>ViewState size, File Upload Memory Leak, Enums passed as Args and CancelEventArgs</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130108</link><pubDate>Wed, 12 May 2004 02:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130108</guid><dc:creator>ShowUsYour</dc:creator><description /></item><item><title>ViewState size, File Upload Memory Leak, Enums passed as Args and CancelEventArgs</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130112</link><pubDate>Wed, 12 May 2004 02:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130112</guid><dc:creator>ShowUsYour</dc:creator><description /></item><item><title>Using Enum in drop downs in a webform</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130304</link><pubDate>Wed, 12 May 2004 10:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130304</guid><dc:creator>DotWind Blog</dc:creator><description /></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130308</link><pubDate>Wed, 12 May 2004 07:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130308</guid><dc:creator>Tim</dc:creator><description>How about using strings in an enum I made a post about that after looking at your post. &lt;a target="_new" href="http://blog.dotwind.com/archive/2004/05/12/150.aspx"&gt;http://blog.dotwind.com/archive/2004/05/12/150.aspx&lt;/a&gt;&lt;br&gt;do you thing there is a way to do what I am trying there?&lt;br&gt;&lt;br&gt;thanks</description></item><item><title>Using Enum in drop downs in a webform</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130311</link><pubDate>Wed, 12 May 2004 10:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130311</guid><dc:creator>DotWind Blog</dc:creator><description /></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130407</link><pubDate>Wed, 12 May 2004 12:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130407</guid><dc:creator>nfactorial</dc:creator><description>Tim,&lt;br&gt;   In application I'm working on required similar functionality. However I wrote an attribute that could be applied to enum members to provide a 'display name' for the entry.&lt;br&gt;&lt;br&gt;ie.&lt;br&gt;&lt;br&gt;public enum MyEnum&lt;br&gt;{&lt;br&gt;    [ EnumDisplayAttribute( &amp;quot;Some Example&amp;quot; )]&lt;br&gt;    SomeExample,&lt;br&gt;}&lt;br&gt;&lt;br&gt;This is a bit more work than it looks like you wanted (have to provide a customized combo-box as well) but works well.&lt;br&gt;&lt;br&gt;Alternately, you could fake it by having a class with public, const strings:&lt;br&gt;&lt;br&gt;public sealed class Example&lt;br&gt;{&lt;br&gt;    private Example() {}&lt;br&gt;&lt;br&gt;    public const string Example = &amp;quot;An Example&amp;quot;;&lt;br&gt;}&lt;br&gt;&lt;br&gt;n!</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130856</link><pubDate>Thu, 13 May 2004 00:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130856</guid><dc:creator>Thomas Eyde</dc:creator><description>How do you verify combinations like Fold + Spindle?</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#130983</link><pubDate>Thu, 13 May 2004 07:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130983</guid><dc:creator>Hugo Rodger-Brown</dc:creator><description>Thomas - you'd be looking to use bitwise logic to verify combinations of enums, for which you need to declare your attribute with the Flags attribute, see below for details:&lt;br&gt;&lt;a target="_new" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemflagsattributeclasstopic.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemflagsattributeclasstopic.asp&lt;/a&gt;&lt;br&gt;</description></item><item><title>re: Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#131011</link><pubDate>Thu, 13 May 2004 09:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:131011</guid><dc:creator>Tim</dc:creator><description>thanks n!&lt;br&gt;&lt;br&gt;I love the idea of the attribute, I will definetly try that.&lt;br&gt;&lt;br&gt;the const strings class is the way I have been doing it. I just found it really easy to bind the enum to the combo box and also to store the values in the database instead of having a different table with the categories I will just put it as an enum and store the int value for it in the table.&lt;br&gt;&lt;br&gt;Tim</description></item><item><title>MBA</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#324452</link><pubDate>Sat, 18 Dec 2004 18:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:324452</guid><dc:creator>MBA</dc:creator><description>Helpful For MBA Fans.</description></item><item><title>Actors and Actresses  &amp;raquo; Archive du blog   &amp;raquo; Eric Gunnerson&amp;#8217;s C# Compendium : Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#6982075</link><pubDate>Fri, 04 Jan 2008 21:46:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6982075</guid><dc:creator>Actors and Actresses  » Archive du blog   » Eric Gunnerson’s C# Compendium : Enums and validation</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://actors.247blogging.info/?p=3784"&gt;http://actors.247blogging.info/?p=3784&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Dinner and a Movie &amp;raquo; Eric Gunnerson&amp;#8217;s C# Compendium : Enums and validation</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#8327891</link><pubDate>Thu, 20 Mar 2008 23:42:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8327891</guid><dc:creator>Dinner and a Movie » Eric Gunnerson’s C# Compendium : Enums and validation</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://dinnermoviesblog.info/eric-gunnersons-c-compendium-enums-and-validation/"&gt;http://dinnermoviesblog.info/eric-gunnersons-c-compendium-enums-and-validation/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> Eric Gunnerson s C Compendium Enums and validation | Green Tea Fat Burner</title><link>http://blogs.msdn.com/ericgu/archive/2004/05/10/129369.aspx#9706917</link><pubDate>Mon, 08 Jun 2009 05:37:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9706917</guid><dc:creator> Eric Gunnerson s C Compendium Enums and validation | Green Tea Fat Burner</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://greenteafatburner.info/story.php?id=3568"&gt;http://greenteafatburner.info/story.php?id=3568&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>