Is it possible to retrieve the ordinal of an enum that has a value?

Consider this:

public enum Foo {

BAR = "foobar"

}

Now if I access the enum with:

var foobar = Foo.BAR;

foobar holds the String "foobar". If I didn't assign the String to the enum it would be 0.


Is there a way to access the ordinal of this enum as in other languages even if I assign a value to the enum??


Something like:


Foo.BAR.ordinal()


  • I don't know the answer, but in some of the other languages it is not wise to use the ordinals in your code, since they can change between builds. Not sure what is your use case but maybe change the string to "0:foobar" and split it. Though it still looks like there might be a better way, maybe if you'd share more details of what you'd like to achieve someone will have a good idea

  • I wanted to use the ordinal for storing a selected enum in the flash and use its string value at runtime. This way I would save a few bytes here and there in the flash. Think of settings that the user can choose from. But looking at this from a new perspective I might have done premature optimization and I should just save the string values to the flash until I really run into flash storage constraints.

    And I guess you are right, that relying on ordinals is a bad idea. I come from Java and ordinals stay the same as long as you maintain their order in the enum declaration. But if someone ever decides to change the order it would cause a lot runtime errors I guess.

    Thanks for your thoughts on this.

  • You are trying to reinvent the wheel... You can use a list in the settings, that does exactly what you want. If you also want to display the strings in the app (not only in the settings) then you can create a const array with the resource IDs of the strings and then load only the string you need to display