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.