qyli Posted January 24, 2011 Posted January 24, 2011 say I have a enum { DUMMY1 = 1, DUMMY2 = 2, }; int v = 2; how can I get "DUMMY2" string from the var v?
ulf.schroeter Posted January 24, 2011 Posted January 24, 2011 say I have a enum { DUMMY1 = 1, DUMMY2 = 2, }; int v = 2; how can I get "DUMMY2" string from the var v? With a dedicated function (as symbolic names will be resolved to int values on compile) String getEnumString( int v ) { if ( v == DUMMY1 ) return "DUMMY1"; else if( v == DUMMY2 ) return "DUMMY2"; else return "INVALID"; }
eugene.litvinov Posted January 24, 2011 Posted January 24, 2011 Or you can use associative array if you have more enumeration elements: enum { DUMMY1 = 1, DUMMY2 = 2, }; string map[]; map[DUMMY1] = "DUMMY1"; map[DUMMY2] = "DUMMY2"; log.message("map[DUMMY1] " + map[DUMMY1] + "\n"); log.message("map[DUMMY2] " + map[DUMMY2] + "\n");
qyli Posted January 25, 2011 Author Posted January 25, 2011 thank you all guys for the reply, they worked, but i don't want write these manully, acctually i want something simlar runtime reflection, as engine known the enum constant value and name, i think there a way to get the enum name from value, for example namespace Example{ enum { DUMMY1 = 1, DUMMY2 = 2, }; } int v = 2; String name = Example.getConstantName(v);
ulf.schroeter Posted January 25, 2011 Posted January 25, 2011 acctually i want something simlar runtime reflection sorry, not available. You have to do a manual string lookup as described.
Recommended Posts