View comments | RSS feed

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

A second technique for creating enumerations also involves creating a separate class with static properties for the enumeration. This technique differs, however, in that each of the static properties contains an instance of the class instead of a string or integer value. For example, the following code creates an enumeration class for the days of the week:

public final class Day
{
    public static const MONDAY:Day = new Day();
    public static const TUESDAY:Day = new Day();
    public static const WEDNESDAY:Day = new Day();
    public static const THURSDAY:Day = new Day();
    public static const FRIDAY:Day = new Day();
    public static const SATURDAY:Day = new Day();
    public static const SUNDAY:Day = new Day();
}

This technique is not used by the Flash Player API but is used by many developers who prefer the improved type checking that the technique provides. For example, a method that returns an enumeration value can restrict the return value to the enumeration data type. The following code shows not only a function that returns a day of the week, but also a function call that uses the enumeration type as a type annotation: [Example modified: 02/04/2008]

function getDay():Day
{
    var date:Date = new Date();
    var retDay:Day;
    switch (date.day)
    {
        case 0:
            retDay = Day.SUNDAY;
            break;
        case 1:
            retDay = Day.MONDAY;
            break;
        case 2:
            retDay = Day.TUESDAY;
            break;
        case 3:
            retDay = Day.WEDNESDAY;
            break;
        case 4:
            retDay = Day.THURSDAY;
            break;
        case 5:
            retDay = Day.FRIDAY;
            break;
        case 6:
            retDay = Day.SATURDAY;
            break;
    }
    return retDay;
}

var dayOfWeek:Day = getDay();

You can also enhance the Day class so that it associates an integer with each day of the week, and provides a toString() method that returns a string representation of the day. You might want to enhance the Day class in this manner as an exercise.


Flash CS3


Comments


vinnyguido said on Jan 31, 2008 at 9:06 AM :
This enumeration using the class itself as the value of each static const is very cool. I like the added type-checking. I had to write the example before I understood how each static const was a different value (since they all look the same). Even though each one is a call to the same constructor, each static const is a unique object. This means you can test for the value as well:

if( dayOfWeek == Day.MONDAY ) ...

By the way, the Date class' day( ) method uses 0 for Sunday not Monday so to use this example effectively you'll have to change the constants from 0-6 using Sunday to Saturday, not Monday to Sunday.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000065.html