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?

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
  • Use `Enum.Parse` like this: `(myclass.myenum)Enum.Parse(typeof(myclass.myenum), "Selected text")` – a-ctor May 15 '16 at 09: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). –  May 15 '16 at 09:52

1 Answers1

1

just cast your int value

(myclass.myenum)intvalue;
Peuczynski
  • 4,591
  • 1
  • 19
  • 33
  • 2
    Just keep in mind that this only works when all the enum values are sequential and start with `0` as the first value. – a-ctor May 15 '16 at 09:56
  • And I think it would be good if you looked into this post http://stackoverflow.com/questions/29482/cast-int-to-enum-in-c-sharp – Peuczynski May 15 '16 at 10:01
  • @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 May 15 '16 at 10:16
  • 1
    @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. – a-ctor May 15 '16 at 10:19