Home > .NET, .NET 2.0, C# 2.0 > Finding an Enum value based on its text

Finding an Enum value based on its text

January 4, 2007

I’ve been a fan of custom Enums since I started coding for .Net: like most software packages, we have tons of selection lists. Many of these are table driven, code-based lists like so:

Code : Description
10 : Asparagus
12 : Carrot
73 : Tomato

etc, etc.

Naturally, I don’t want my users to have to know that 73 means Tomato, so like a good little developer, I put these values into an Enum:

public enum Vegetables
{
    Asparagus = 10,
    Carrot = 12,
    Tomato = 73
}

Then I use that Enum to populate a ComboBox on my Form. To do so, I utilize the Enum class’s GetNames method.

foreach (string s in Enum.GetNames(typeof(MyClass.Vegetables)))
{
    this.cboxVegetableNames.Items.Add(s);
}

The trick here is to point the typeof parameter to the full class designation of the enumeration you want to list. This method returns an IEnumerable you can use to loop through the names.

So far so good: pretty straight forward and probably something most of us have already figured out. The problem I had recently was that I needed the Enum value in order to properly update a database. In other words, when the user selected Tomato, I needed 73. Getting the int value of an enum is easy enough via a simple cast:

MyClass.Vegetables veggie = MyClass.Vegetables.Tomato;
int veggieValue = (int)veggie;

So if I could figure out which Enum value was selected, I could easily get the int. The problem was, I only had the Text string from the ComboBox to go off of. I suppose I could do some sort of brute force loop through all the Enum members until I found a matching string, but that seems like overkill. It would be especially bad if I had lots of options in the ComboBox and lots of ComboBoxes.

Fortunately, I was able to find something a little better built right in to the Enum class:

MyClass.Vegetables veggie = (MyClass.Vegetables)Enum.Parse(typeof(MyClass.Vegetables), this.cboxVegetableNames.SelectedItem.ToString());
int veggieValue = (int)veggie;

It’s definitely a little ugly, but gets the job done. The trick to getting this to work is the cast into an Enum variable of the same type. Again we use typeof() to get a reference to the full class name of the Enum and then cast the results into an int.

Now you can easily work with Enums to put the list of strings into a ComboBox and later retireve the Enum value from the selected string. In a future article, I’ll demonstrate how you can use attributes to show different text than the actual member name.

Advertisements
Categories: .NET, .NET 2.0, C# 2.0
  1. Rudedog2
    November 17, 2008 at 2:49 pm

    You are storing the enums as strings, which is not necessary. Keep in mind that Items is an array of System.Objects. The control uses the inherited ToString() method to display the objects in Items. I suggest that you store the objects directly, instead.

    foreach (Vegetables obj in Enum.GetValues(typeof(Vegetables)))
    {
    ;
    this.comboBox1.Items.Add(obj);
    }

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: