User Guide

124 Object-Oriented Programming in ActionScript
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:
function getDay():Day
{
var date:Date = new Date();
var retDay:Day;
switch (date.day)
{
case 0:
retDay = Day.MONDAY;
break;
case 1:
retDay = Day.TUESDAY;
break;
case 2:
retDay = Day.WEDNESDAY;
break;
case 3:
retDay = Day.THURSDAY;
break;
case 4:
retDay = Day.FRIDAY;
break;
case 5:
retDay = Day.SATURDAY;
break;
case 6:
retDay = Day.SUNDAY;
break;