Home > DeveloperSection > Forums > Assigning enum in c# with a string
Tanuj Kumar

Total Post:134

Points:940
Posted on    December-23-2013 2:31 AM

 C# C# 
Ratings:


 1 Reply(s)
 841  View(s)
Rate this:

I was wondering if there's an option to sign an enum 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!



Pravesh Singh

Total Post:411

Points:2881
Posted on    December-23-2013 5:01 AM

hi Tanuj,

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.


Don't want to miss updates? Please click the below button!

Follow MindStick