- Home /
Compare 2 enum variables method?
I simply want to check whether the state has been changed, like this:
var currentState:states;
var lastState:states;
if(lastState != currentState){
print("state changed");
lastState = currentState;
}
But I get error, it seems this comparison is somehow wrong? but how to correct this comparison.
Regards,
Answer by Landern · Dec 11, 2012 at 02:40 PM
I assume you didn't set the flags on the enum.
Try comparing the ints of the enum.
var currentState:states;
var lastState:states;
if((int)lastState != (int)currentState){
print("state changed");
lastState = currentState;
}
Thanks jordan, just a question...is this an efficient way of comparing, if this app is designed for mobile? performance wise, is it advised? thanks
Yes, by default when you create an enum, it defaults to each enum value to an int, starting with 0 on the top item in the list of enum values. This can be overridden to use different types or start the numbering at a different point, or you can explicitly set each and every value in the enum if you want. But the performance is fast, yes.
and my final question, this is in C# right? what would be the javascript conversion, thanks a ton. I've been searching the net not yet found what is the JSCript version of (int)State...