GeekZilla
Using Enum.Parse()
Have you ever written any code along the lines of...
myEnum GetEnumValue(string EnumString) { myEnum enumValue; switch (EnumString) { case "Value1": enumValue = myEnum.Value1; break; case "Value2": enumValue = myEnum.Value2; break; else: enumValue = myEnum.ValueNone; } return enumValue; }
Embarrassingly, I did. Until I realised that the Enum class had a static method called Parse that can be used as follows...
myEnum GetEnumValue(string EnumString) { return (myEnum)Enum.Parse(typeof(myEnum),EnumString); }
Easy! Robust! and all that.
Author Dave Howard
I have been involved in IT development for the last 10 years - a lot of it around desktop applications and telecoms.
Comments
Paul
said:
Brilliant!
Ben
said:
Thanks, I have to look this up every time I need it.
Just one thing, why would you call your Enum "myEnum", even for this example surely it should by "MyEnum"?
Ben
Add Comment
Tag Cloud
ajax
asp.net
atlas
authentication
Beta
c#
clr
CSS
dataset
datetime
dojo
dts
email
encryption
enum
event validation
excel
favicon
filesystem
fileupload
flash
fofo
form
full text search
fulltext
Gadget
gaming
gdi
GET
Globalization
javascript
navigation
PaulMarshall
POST
regex
security
seo
sitemap
sql
stored procedure
Strongly Typed Dataset
test
thanks
tips
url
utorrent
validation
vb
vb.net
Vista
VM
VMWare
vs2005
web2
webservice
windows forms
WSS
xml
xna
