I was wondering if there's an option to sign an enum, for say, color, with the options of white black and red, directly with a string "red". without using if's... I mean i don't want to write:
if (string1=="red") {mycolor=Color.red};
thanks!
You can use the Parse method from the Enum class:
mycolor = (Color)Enum.Parse(typeof(Color), string1, true);
Color ColorEnum = (Color)Enum.Parse(typeof(Color), "YourValue");
or
Color ColorEnum = (Color)Enum.Parse(typeof(Color), "YourValue",true);
This Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
In second method, the Bool specifies whether the operation is case-insensitive