How to match int to enum

Sachchidanand picture Sachchidanand · Nov 3, 2011 · Viewed 42.6k times · Source

I am receiving return value in the form of long or int from Native code in Android, which I want to convert or match with enum, for processing purpose. Is it possible ? How?

Answer

Kevin Galligan picture Kevin Galligan · Nov 3, 2011

If you have full control of values and enums, and they're sequential, you can use the enum ordinal value:

enum Heyo
{
  FirstVal, SecondVal
}

...later

int systemVal = [whatever];
Heyo enumVal = Heyo.values()[systemVal];

int againSystemVal = enumVal.ordinal();