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.
The Enumerating data types in Actionscript 3 (AS3) by Joel Hooks, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 3.0 United States License.