I was hoping to get some help with this. I need a function that can take in a string that is a key for a dictionary and an enum type that is has to be cast to.
The dictionary key will be a number that corresponds to an enum. I need to know how to cast the int into an enum where the enum is variable.
Here is the function as I have it written. This might be more clear than my explanation.
string GetEnum(string keyName, Enum enumType)
{
var defaultOut = "";
Model.Form.TryGetValue(keyName, out defaultOut);
if(defaultOut != ""){
return EnumDescriptionUtility.GetDescription((enumType)Convert.ToInt32(defaultOut));
}
else{
return defaultOut;
}
}
I have used most of this code before. the difference before was that enumType was hard coded to the actual enum. I want to offload all that repetition to a function.
Any help is appreciated.
TryGetValue)?defaultOutwill always be passed toConvert.ToInt32, why is it a string at all? Why not just an int? Or, like i said, why not make it an enum?