Converting a string to an enum

I needed to take a string value and convert it to an enum for further processing. My enum was defined like so:

    public enum PinColor {
        white = 0,
        green = 1,
        yellow = 2,
        red = 3

I needed to do some calculations based on the color of the pin that was returned, which was done in a function (not shown). So to return the proper enumeration value for the color red, I needed to use the Enum.Parse method, casting the return value and parameter to my enumeration:

PinColor test = (PinColor)Enum.Parse(typeof(PinColor), "red", true);


  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: