Skip to content

Parsing a DropDownList Item to an Enum

February 14, 2012

Have the trouble of parsing a dropdown list item containing the value you widh to parse to Enum object?

During implementation I had used Enum.ToObject to convert the selected value as an integer into the enum object, but that is a restrictive to convert to your specified Enum object (e.g. CustomUnit object) as the value may not be an integer at all.

object.CustomUnit = (CustomUnit)Enum.ToObject(typeof(CustomUnit), Convert.ToInt32(ddlCustomUnit.SelectedValue)); – old

Here’s the better solution: –

object.CustomUnit = (CustomUnit)Enum.Parse(typeof(CustomUnit), ddlCustomUnit.SelectedItem.Text, true);

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: