Enumerations in Actionscript 3

5 11 2009

Coming from a .Net sort of background, when typing in ‘enum’ into an Actionscript 3 file, I was baffled when it didn’t turn blue! Enumerations are such useful things, I was surprised they weren’t supported in every programming language!

However in Actionscript 3 there is a way to create a class that mimics an enumeration using static constant variables. The code is as follows:

public class CountryCode
{
    public static const UK:int = 1;
    public static const US:int = 3;
    public static const AUS:int = 4;
}

You can then access the “enumeration” as normal:

var whichCountry:int = CountryCode.UK;

Advertisements