0

First, I apologize if this is a duplicate question. I have been searching for too long to no avail.

Assume I have two Enums:

public enum Dogs
{
    Mastiff,
    Bulldog
}

public enum Cats
{
    Manx,
    Tiger
}

Based on a user selection of "Cats" or "Dogs" from a ComboBox, I want to populate another ComboBox with the appropriate Enum values. This can be done with a method like this:

void PopulateComboBox<EnumType>(RadComboBox box, Enum displayUnits)
{
    // values and text derived from enumExtension methods
    foreach (Enum unit in Enum.GetValues(typeof(EnumType)))
    {
        var item = new RadComboBoxItem(unit.GetName(), unit.ToString());
        item.ToolTip = unit.GetDescription();
        if (displayUnits != null && unit.ToString() == displayUnits.ToString())
            item.Selected = true;
        box.Items.Add(item);
    }
}

How do I get the correct EnumType from the user specified string value so I could call it like as such (bonus points if I can specify the 'displayUnits' parameter to force a desired selection):

string choice = myComboBox.SelectedValue;
?? choiceAsAnEnumType = choice variable converted in some way ??
PopulateComboBox<choiceAsAnEnumType>(outputComboBox, null);

This implementation would be useful as I have a large number of Enums in my current project. Currently, I'm having to do switch (choice) and pass in the appropriate Enum Type in the various cases.

Any variations on the method are acceptable as I'm in no way locked into this strategy (outside of the time to implement any others).

Edit: To explain away the TryParse/Parse suggestion, I am not interested in getting the enum value (Mastiff or Bulldog) from a string; rather I want the certain flavor of Enum (Dogs or Cats) from a string. TryParse requires a supplied T where I do not know T in my case. Apologies if I have misunderstood the methods provided as examples for TryParse, I'm relatively new to C# and ASP as a whole.

rosst
  • 177
  • 10
  • 3
    use `Enum.TryParse` https://msdn.microsoft.com/en-us/library/dd783499%28v=vs.110%29.aspx – chomba Apr 17 '15 at 21:45
  • possible duplicate of [Parse string to enum type](http://stackoverflow.com/questions/1424971/parse-string-to-enum-type) – crthompson Apr 17 '15 at 21:46
  • Why not add the enum itself to the combo box (rather than calling `unit.GetName`) then you can access it via the `SelectedItem` property rather than the `SelectedValue` property? – christophano Apr 17 '15 at 21:49
  • @christophano because I was not aware you could do that lol. Looks like I'm going to have to do some work over the weekend to test this stuff out. I appreciate the responses. – rosst Apr 17 '15 at 23:03
  • @chomba I'm pretty sure the TryParse is used to get a value out of the enum based on a string. At least that's what I'm taking from that example. – rosst Apr 17 '15 at 23:06

2 Answers2

0

You can load a type from a string to pass to your method like this. You wouldn't use the generic on the populate method.

class AnimalOptions
{
    public enum Dogs
    {
        Mastiff,
        Bulldog
    }

    public enum Cats
    {
        Manx,
        Tiger
    }
}


Type t = typeof(AnimalOptions);
Type enumType = t.GetNestedType("Dogs");
Populate(enumType);
jones6
  • 704
  • 5
  • 7
  • Is there a way to do this without wrapping them all in a class? Currently they're all just individual files (Dogs.cs, Cats.cs, etc). Though I will give this a shot if not. Not much effort needed to copy/paste enums. – rosst Apr 17 '15 at 23:00
  • You can use reflection if want to load it from the current assembly. `Assembly.GetExecutingAssembly().GetTypes().First(x => x.Name == "Dogs");` – jones6 Apr 17 '15 at 23:21
  • Oo, nice. Is the reflection method drastically more taxing on load times? – rosst Apr 17 '15 at 23:32
  • Reflection is slower, but I would test how it performs for you and your use case. I haven't had a performance problem with it personally. – jones6 Apr 17 '15 at 23:46
0

I would load your first combo box with the enum types like this:

firstComboBox.Items.Add(new RadComboBoxItem(typeof(Dogs), typeof(Dogs).Name));
firstComboBox.Items.Add(new RadComboBoxItem(typeof(Cats), typeof(Cats).Name));

Then, called from the selected item changed event on the first combo box:

secondComboBox.Items.Clear();
foreach (var value in Enum.GetValues(firstComboBox.SelectedValue))
{
    secondComboBox.Items.Add(new RadComboBoxItem(value, value.ToString()));
}
christophano
  • 915
  • 2
  • 20
  • 29