Geeks With Blogs
INETA Pakistan Blogs Home of INETA Geeks

To compare strings with enums the right way, one should use enum.parse rather than comparing string literals. Following is an example where i am parsing a string type variable named messageId and creating an enum of type MessageType

MessageType messageType = (MessageType) System.Enum.Parse(typeof(MessageType),messageId,true);

Last argument in System.Enum.Parse specifies if the enumeration/comparison should be case sensitive or not.


Hammad Rajjoub,
MVP (Windows Server System - XML Web Services),
User Group Leader - Dot Net Wizards (
http://dotnetwizards.blogspot.com),
Chariman UG Relations Committee (
http://inetapakistan.org),
Member Speakers Bureau (
http://mea.ineta.org) Posted on Tuesday, August 9, 2005 4:09 PM | Back to top


Comments on this post: System.Enum.Parse :: The right way!

# re: System.Enum.Parse :: The right way!
Requesting Gravatar...
How to do the same thing with VC++.net. My problem is that in VC++.net enum::parse returns Object pointer which is not converting to the enumeration variable
Left by Rohit on Oct 03, 2007 8:42 AM

# re: System.Enum.Parse :: The right way!
Requesting Gravatar...
Just what i was looking for, thx
Left by Shikyo on Jan 29, 2008 3:49 AM

# re: System.Enum.Parse :: The right way!
Requesting Gravatar...
Hi, there is also a more elegant way to do it:

http://ko-sw.blogspot.com/2009/03/elegant-way-to-parse-c-enums.html
Left by Kirill Osipov on Mar 26, 2009 3:28 AM

Your comment:
 (will show your gravatar)


Copyright © INETA Pakistan | Powered by: GeeksWithBlogs.net