x


Converting a string to an enum

Hi! I need to convert a string into its equivalent enum object. Although I could make an auxiliary function to do that, I wonder if C# can do that for me and I've found this article, but I'm afraid it doesn't work in Unity.

I've been searching here through Unity Answers but haven't been able to find a solution, can you help me?

Thanks in advance!

more ▼

asked Mar 14 '11 at 12:56 PM

Ignacio 1 gravatar image

Ignacio 1
56 8 8 14

That does work. Let me see if I can find an example for you.

Mar 14 '11 at 01:01 PM SirGive

Yes, sorry about being a little bit vague. I should have mentioned I was getting a compilation error because I was trying to use it exactly as it appeared in the article (i.e., by using Enum.Parse). I tried the solution that taoa suggested and it worked! Thanks for your reply!

Mar 14 '11 at 01:33 PM Ignacio 1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest
YourEnumType parsed_enum = System.Enum.Parse( typeof( YourEnumType ), your_string );

This works!

more ▼

answered Mar 14 '11 at 01:09 PM

taoa gravatar image

taoa
1.6k 8 13 35

Great! Many thanks!

Mar 14 '11 at 01:32 PM Ignacio 1

where does the string go?

Jan 30 at 03:57 AM Anxowtf

@Anxowtf

The initial string goes into the 'your_string' variable in the example code above. The function then looks for an entry in the specified enumeration that matches the given string and returns it.
Imagine the opposite of enum.ToString().

Jun 03 at 06:36 PM Zarenityx
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x430
x132

asked: Mar 14 '11 at 12:56 PM

Seen: 2764 times

Last Updated: Jun 03 at 06:37 PM