You need to parse the string values into the enum values using Enum.Parse
:
private addFruit( string fruit, string color )
{
Fruits parsedFruit = (Fruits) Enum.Parse(typeof(Fruits), fruit);
Colors parsedColor = (Colors) Enum.Parse(typeof(Colors), color);
myDictionary.Add(parsedFruit, parsedColor);
}
This will throw an error if the string value does not match any of the enum values (i.e. it is null
, empty or a string that isn't in the enum).
In .Net 4.5, there is a TryParse
method that may be more appropriate than just Parse
.