Enums Language Reference

Summary

Enums is a language extension that adds an enumerated type support to ActionScript3.

The enumerated type consist of a set of named values. For example, the four suits in a deck of playing cards may be four enumerators named  SPADES, CLUBSDIAMONDSHEARTS, belonging to an enumerated type named Cardsuit.

Syntax

enum enumTypeName [ implements Interface ] { ENUM1 [ , ENUM2, ENUM3 … ] }

Enum class declaration can be created the from the right-click menu on the package after importing the enum language to a project. The syntax is similar to a class declaration statement. Enumerated members should be written in capital letters and separated by commas.

public enum Cardsuit implements <none> { 

  SPADES, 
  CLUBS, 
  DIAMONDS, 
  HEARTS 

  public function Cardsuit() { 
    // enum constructor
  } 
}

enumTypeName . ENUM [ . properties ]

An enumerator can be referred from another class using an expression, containing enumerated type name, enumerator and its properties.

public function Main() {
  Cardsuit.SPADES; 
  Cardsuit.CLUBS.index;
} 

function enumTypeName ( parameters )

Enum constructor, as well as any other function, can recieve arguments. Besides the constructor, an enumerator can have another methods, as well as any other class.

public enum Cardsuit implements <none> { 
  SPADES(36), 
  CLUBS(36), 
  DIAMONDS(36), 
  HEARTS(36)

  public function Cardsuit(index:int) { 
    // this will be executed on instance creation 
    this._index = index; // create field and assign a parameter 
  }

  private var _index : int;

  public function get index (  ) : int { 
    // getter
    return this._index; 
  } 
}

name operation

Name operation is often used when is needs to get enumerators from any other method.

public function Main() { 
  myGetEnum(Cardsuit.DIAMONDS);
} 

public function myGetEnum(myEnum : Cardsuit) : void { 
  myEnum.name; // myEnum.name = DIAMONDS
  Cardsuit.NAMES; // list, that stores all enums of Cardsuit class 
  myEnum.index; // DIAMONDS.index value
}

switch enumTypeName case ENUM

Using switch statement is a good practice with enums, because case expression allows to use short form to refer to an enumerator.

switch (myEnum) { 
  case SPADES : 
    break; 
  case CLUBS : 
    break; 
  case DIAMONDS : 
    break; 
  case HEARTS : 
    break; 
}