<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/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>Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx</link><description>Here&amp;#39;s a nice library function useful when loading enum-typed properties from your database: public static T ToEnum&amp;lt;T&amp;gt;(int typeValue) { return (T)Enum.ToObject(typeof(T), typeValue); }</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx#2364945</link><pubDate>Tue, 24 Apr 2007 21:05:01 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2364945</guid><dc:creator>Yang</dc:creator><author>Yang</author><description>&lt;p&gt;Be careful about the performance of enum operations.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2364945" width="1" height="1"&gt;</description></item><item><title>re: Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx#2227503</link><pubDate>Fri, 13 Apr 2007 10:02:25 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2227503</guid><dc:creator>PaulWilson</dc:creator><author>PaulWilson</author><description>&lt;p&gt;You can call Enum.IsDefined in the helper function first to guarantee a valid value, and if it fails then throw an exception. &amp;nbsp;The same problem occurs with the simple cast syntax, so at least using a helper function makes it easier to add extra things like this when you find it is needed.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2227503" width="1" height="1"&gt;</description></item><item><title>re: Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx#2226503</link><pubDate>Fri, 13 Apr 2007 06:29:39 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2226503</guid><dc:creator>Didasoft</dc:creator><author>Didasoft</author><description>&lt;p&gt;all these helper function can not ensure valid value of enum.&lt;/p&gt;
&lt;p&gt;at .net 2.0, &lt;/p&gt;
&lt;p&gt;MyEnum test = ToEnum&amp;lt;MyEnum&amp;gt;(value);&lt;/p&gt;
&lt;p&gt;support MyEnum only have two enum value, and the value is 10, no exception will throw. So i don't know MyEnum test now have a invalid value.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2226503" width="1" height="1"&gt;</description></item><item><title>re: Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx#2218938</link><pubDate>Thu, 12 Apr 2007 16:26:40 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2218938</guid><dc:creator>Dave T</dc:creator><author>Dave T</author><description>&lt;p&gt;I would throw something like this into your helper:&lt;/p&gt;
&lt;p&gt;System.Diagnostics.Debug.Assert(typeof(T).IsEnum);&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2218938" width="1" height="1"&gt;</description></item><item><title>re: Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx#2208886</link><pubDate>Tue, 10 Apr 2007 20:37:51 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2208886</guid><dc:creator>PaulWilson</dc:creator><author>PaulWilson</author><description>&lt;p&gt;As for the usage, consider an enum named MyEnum and a variable of that enum type that you want to load from an int value, possibly one you've stored in the database -- just do the following:&lt;/p&gt;
&lt;p&gt;MyEnum test = ToEnum&amp;lt;MyEnum&amp;gt;(value);&lt;/p&gt;
&lt;p&gt;As for the direct cast, that's a good question, and I could have sworn I'd tried that many times and it didn't work -- although it did just now in my test. &amp;nbsp;Maybe I'm thinking of a limitation I encountered in .net v1 that I've been working around -- but maybe I'm wrong there as I didn't retest that assumption. &amp;nbsp;Oh well, it seems that its not necessary for .net v2 at any rate, and if you have generics then you have .net v2, so mute point I suppose.&lt;/p&gt;
&lt;p&gt;Thanks for the comments.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2208886" width="1" height="1"&gt;</description></item><item><title>re: Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx#2208842</link><pubDate>Tue, 10 Apr 2007 20:27:12 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2208842</guid><dc:creator>Christian</dc:creator><author>Christian</author><description>&lt;p&gt;Can you demo the usage?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2208842" width="1" height="1"&gt;</description></item><item><title>re: Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx#2208817</link><pubDate>Tue, 10 Apr 2007 20:20:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2208817</guid><dc:creator>Steven</dc:creator><author>Steven</author><description>&lt;p&gt;I've been looking at the implementation of Enum.ToObject(Type, int) (using Lut'z Reflector, of course), but can't seem to think of a reason to use it. Why not cast the int directly to it's enum?&lt;/p&gt;
&lt;p&gt;MyEnum = (MyEnum)typeValue;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2208817" width="1" height="1"&gt;</description></item><item><title>re: Function to Load Enum-Typed Properties from Database</title><link>http://weblogs.asp.net/pwilson/archive/2007/04/10/function-to-load-enum-typed-properties-from-database.aspx#2207887</link><pubDate>Tue, 10 Apr 2007 14:51:57 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2207887</guid><dc:creator>jayson knight</dc:creator><author>jayson knight</author><description>&lt;p&gt;To further constrain the input for T, add a type constraint of where T: struct. You can't use : enum, so struct is the next best thing.&lt;/p&gt;
&lt;p&gt;Another handy utility enum function I use quite often:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static T ParseEnum&amp;lt;T&amp;gt;(string value) where T: struct&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (T)Enum.Parse(typeof(T), value, true);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2207887" width="1" height="1"&gt;</description></item></channel></rss>