Enumerating data types in Actionscript 3 (AS3)

In computer programming, an enumerated type is an abstract data type used to model an attribute that has a specific number of options (or identifiers) such as the suit of a playing card (i.e. a Club, Diamond, Heart or Spade). Using this type allows the program to handle the attribute more efficiently than a string while maintaining the readability of the source code.
-from Wikipedia

I’ve found this method (illustrated in code below) for enumeration of datasets to be useful, and at this point even essential, in my applications. Prior to this I was actually cutting and pasting the arrays that I was using in my various combo boxes and other selection components as dataProviders. By breaking the dataset into an enumeration class, it makes it easy to maintain the list and lets the dataset function as a first class citizen, ensuring that your data is consistent throughout the application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package enum
{
	/**
	 * CloudTypeEnum enumerates cloud types.
	 * @author JHooks
	 *
	 */
	public class CloudTypeEnum
	{
		public static const NONE:CloudTypeEnum			= new CloudTypeEnum( "None",	   -1 );
		public static const CUMULUS:CloudTypeEnum		= new CloudTypeEnum( "Cumulus", 	0 );
		public static const STRATUS:CloudTypeEnum		= new CloudTypeEnum( "Stratus", 	1 );
		public static const CIRRUS:CloudTypeEnum		= new CloudTypeEnum( "Cirrus", 		2 );
		public static const NIMBUS:CloudTypeEnum		= new CloudTypeEnum( "Nimbus", 		3 );
 
		public var value:String
		public var ordinal:int
 
		/**
		 * CloudTypeEnum constructor
		 * @param value
		 * @param ordinal
		 *
		 */
		public function CloudTypeEnum( value:String, ordinal:int )
		{
			this.value = value;
			this.ordinal = ordinal;
		}
 
		/**
		 * A list of cloud types
		 * @return
		 *
		 */
		public static function get list( ):Array
		{
			return [ CUMULUS,
					 STRATUS,
					 CIRRUS,
					 NIMBUS ];
		}
 
		/**
		 * A list of cloud types appropriate for use in ComboBox and other
		 * selection components as a DataProvider
		 *
		 * myComboBox.dataProvider = CloudTypeEnum.cList;
		 * @return
		 *
		 */
		public static function get cList( ):Array
		{
			return list.unshift( NONE );
		}
 
		/**
		 * Select a cloud type by its value property
		 * @param value
		 * @return
		 *
		 */
		public static function selectByValue( value:String ):TaskTypeEnum
		{
			for each ( var cloudType:TaskTypeEnum in CloudTypeEnum.list )
			{
				if ( value == cloudType.value )
					return cloudType;
			}
 
			return NONE;
		}
	}
}

Thanks to Cliff Hall who showed this to me via one of his PureMVC demo applications. For the life of me I can’t remember which one.

  • Bjorn Schultheiss

    What about if you wanted to use a value from a resource bundle to set value:String ? where would you initialize the resourceManager ?

    eg new CloudTypeEnum( resourceManager.getString(“myBundle”, “NONE”), -1 );

  • Joel

    I would just use a straight VO in that case. This is a ‘static’ list of items. You don’t ever call new CloudTypeEnum(), you just access the static vars, which instantiate themselves as CloudTypeEnum objects.

  • Bjorn Schultheiss

    Cool, thats how i’ve been doing it so far.

  • https://puremvc.org Cliff Hall

    There are some Enums in the PureMVC EmployeeAdmin Flex demo:

    -=Cliff>

  • Joel

    Thanks Cliff, I knew it was there someplace. I really dig this method. Keeps things nice and DRY.

  • Nick

    Copy and paste errors? I think “TaskTypeEnum” in the above code should be “CloudTypeEnum”.

  • https://www.facebook.com/RedTuttle Chuck Reed

    Hey, I found a simpler way to do this. 

            //At the end, LENGTH will be equal to the number of items.  Great to use as a sentinel value in a loop, or an initiator for an array that contains exactly one value for each member of the enum.
            public static var LENGTH            :int                     = 0;
            public static var FIRST            :int                     = LENGTH++;
            public static var SECOND        :int                     = LENGTH++;
            public static var THIRD           :int                     = LENGTH++;
            public static var FOURTH        :int                     = LENGTH++;

    What do you think?  Personally, I think this approach works great in computationally intensive tasks, where instantiating and referencing a class for each member would incur undesirable overhead, like in encoding or real-time bitmap rendering.