User Guide
Classes 123
Enumerations with classes
Enumerations are custom data types that you create to encapsulate a small set of values.
ActionScript 3.0 does not support a specific enumeration facility, unlike C++ with its
enum
keyword or Java with its Enumeration interface. You can, however, create enumerations using
classes and static constants. For example, the PrintJob class in the Flash Player API uses an
enumeration named PrintJobOrientation to store the set of values comprising
"landscape"
and
"portrait", as shown in the following code:
public final class PrintJobOrientation
{
public static const LANDSCAPE:String = "landscape";
public static const PORTRAIT:String = "portrait";
}
By convention, an enumeration class is declared with the final attribute because there is no
need to extend the class. The class comprises only static members, which means that you do
not create instances of the class. Instead, you access the enumeration values directly through
the class object, as shown in the following code excerpt:
var pj:PrintJob = new PrintJob();
if(pj.start())
{
if(pj.orientation == PrintJobOrientation.PORTRAIT)
{
...
}
...
}
All of the enumeration classes in the Flash Player API contain only variables of type String,
int, or uint. The advantage of using enumerations instead of literal string or number values is
that typographical mistakes are easier to find with enumerations. If you mistype the name of
an enumeration, the ActionScript compiler generates an error. If you use literal values, the
compiler does not complain if you spell a word incorrectly or use the wrong number. In the
previous example, the compiler generates an error if the name of the enumeration constant is
incorrect, as the following excerpt shows:
if(pj.orientation == PrintJobOrientation.PORTRAI) // compiler error
However, the compiler does not generate an error if you misspell a string literal value, as
follows:
if(pj.orientation == "portrai") // no compiler error