0

I have a dropdown made with the new UI system; I can read the values of the enum, and populate the dropdown without problems.

foreach (myclass.myenum the_enum in myclass.myenum.GetValues(typeof(myclass.myenum)))
{
    the_dropdown.options.Add(new Dropown.OptionData() { text = the_enum.ToString()});
}

Now, when I read the value though, I get back the int value related to the selected entry. Is there a way to get the value as enum or text; instead than as int?

2
  • Use Enum.Parse like this: (myclass.myenum)Enum.Parse(typeof(myclass.myenum), "Selected text") Commented May 15, 2016 at 9:41
  • Is this working in .Net 2.6? I can't see Enum in code completion. It just give me the enum datatype (not capital). Commented May 15, 2016 at 9:52

1 Answer 1

1

just cast your int value

(myclass.myenum)intvalue;
Sign up to request clarification or add additional context in comments.

4 Comments

Just keep in mind that this only works when all the enum values are sequential and start with 0 as the first value.
And I think it would be good if you looked into this post stackoverflow.com/questions/29482/cast-int-to-enum-in-c-sharp
@widi why would a cast to the enum only work for sequential enums? As long as the intvalue is a valid value in the enum, this always works.
@derpirscher yes an valid int value would work, but in this context the int value would be gathered from the dropdown, which would return a value from 0 to n. Therefore all the values must be sequential and start with a 0 in order to produce the same results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.