Using Enums in AS3
Enums are my favorite community developed language feature!!!
UPDATED: I have incorporated all comments into a new version of my demo, and updated the post and source code! Old comments are cleared.
An Enum is a kind of variable that is not natively available in ActionScript 3.0, but can be found in other languages such as C++. Perhaps Enums will be included in the upcoming ActionScript 4.0 when it is released.
Basically – An Enum is a type with a restricted set of values. Typically developers use a string to hold simple state variables such as this;
//USING STRINGS (NOT ENUMS) public static var STATE_ENABLED : String = "STATE_ENABLED"; public static var STATE_DISABLED : String = "STATE_DISABLED"; private var _currentState_str : String = STATE_ENABLED;
Is this familiar? I’m sure it is. By using the static variable the benefit is that developers are less likely to misspell the name of one of these values due to helpful authortime and compile time errors. However the type is stored as a simple string so the developer *CAN* easily pass the wrong string in there (because ANY string is ‘correct’ to the compiler’). This is where Enums are great. Enums would work like this;
//USING ENUMS
// 1. STRONG TYPED & NICE USAGE
var current_estate : StateEnum = StateEnum.Connecting;
// traces 'Connecting'. Nice!
trace ("Instance : " + current_estate);
// traces 'true'
trace ("TypeCheck : " + (current_estate is StateEnum) );
// trace 'true' - This is the most common usage.
// Checking if a StateEnum instance matches a kind of StateEnum
trace ("Match? : " + (current_estate == StateEnum.Connecting) );
// 2. PROPERLY THROWS AN ERROR - YOU CAN'T INSTANTIATE StateEnum Outside of StateEnum.as
//var local_enum : StateEnum = new StateEnum ();
Next Steps
- Don’t understand me? I don’t blame you. It’s crazy. Check out the source (See ‘Member Resources’ below).
Member Resources
You must be a 'Free Member' to see this content. With your access you will enjoy members-only content like this, HD video tutorials, and access to complete source code.
Ready To Join?
- Sign up for 'Free Member' level now. Its free!
- Sign up for 'Paid Member' level to enjoy much, much more! (Invite Only)
- Log in and enjoy!













I tried your enum class approach once. I found several problems with this: Flash requires constructors to be public, and since no actual polymorphism is present, you can’t get away with creating a default constructor and then a private one. The programmer can still instance another EnabledEnum and use it.
Updated.
Now the programmer cannot ‘instance another’ Enum.
this is the closest for as3 i have seen, been worthy of a authors note in the code
In your approach it’s still possible to create a new instance of the enum class and use this value:
current_estate = new StateEnum();
This would brake you code.
Have you seen the Enumerator class of the Temple Library: http://templelibrary.googlecode.com/svn/trunk/modules/common/doc/temple/common/enum/Enumerator.html
With this class it’s not possible to create a new instance of the Enumerator outside of the (extended) Enumerator class.
And it’s even possible to serialize / deserialize the Enumerator using JSON, as you can see in the following example:
http://templelibrary.googlecode.com/svn/trunk/examples/index.html#JSON
Updated.
Now the programmer cannot ‘instance another’ Enum. The unique benefit of your link is the JSON serialization. That is indeed cool and my demo does not do that.
Please have a look at https://github.com/nodename/Ultimate-Enum