There have been plenty of times that Ive needed to reuse the values of my enums as an array. A good example of this would be an enumeration of US states. There are many ways of doing this, however I find that writing an enumeration of static constants gives me the most flexibility and accuracy in my code.
In a recent project I had a user registration form which has a drop down component for the state you live in. Because another area of my application required that I was able to test against a users state I created an enum of the US state. Now in this form I was trying to avoid retyping all of these states into the fields data provider, besides it being tremendously tedious, it breaks the DRY principle.
I remembered from a different conversation I had with paul that there was a way to get actionscript to dump out the contents of a class into an xml list using describeType()
Using the describeType() method I was now able to get all of my constants in my class into a nice little array i could use as a data provider like this:
package com.lejnieks.poc.model.enums { import flash.utils.describeType; public class USAStates { public static const ALABAMA:String = "Alabama"; public static const ALASKA:String = "Alaska"; public static const ARIZONA:String = "Arizona"; public static const ARKANSAS:String = "Arkansas"; public static const CALIFORNIA:String = "California"; public static const COLORADO:String = "Colorado"; public static const CONNECTICUT:String = "Connecticut"; public static const DELAWARE:String = "Delaware"; public static const DISTRICT_OF_COLUMBIA:String = "District Of Columbia"; public static const FLORIDA:String = "Florida"; public static const GEORGIA:String = "Georgia"; public static const HAWAII:String = "Hawaii"; public static const IDAHO:String = "Idaho"; public static const ILLINOIS:String = "Illinois"; public static const INDIANA:String = "Indiana"; public static const IOWA:String = "Iowa"; public static const KANSAS:String = "Kansas"; public static const KENTUCKY:String = "Kentucky"; public static const LOUISIANA:String = "Louisiana"; public static const MAINE:String = "Maine"; public static const MARYLAND:String = "Maryland"; public static const MASSACHUSETTS:String = "Massachusetts"; public static const MICHIGAN:String = "Michigan"; public static const MINNESOTA:String = "Minnesota"; public static const MISSISSIPPI:String = "Mississippi"; public static const MISSOURI:String = "Missouri"; public static const MONTANA:String = "Montana"; public static const NEBRASKA:String = "Nebraska"; public static const NEVADA:String = "Nevada"; public static const NEW_HAMPSHIRE:String = "New Hampshire"; public static const NEW_JERSEY:String = "New Jersey"; public static const NEW_MEXICO:String = "New Mexico"; public static const NEW_YORK:String = "New York"; public static const NORTH_CAROLINA:String = "North Carolina"; public static const NORTH_DAKOTA:String = "North Dakota"; public static const OHIO:String = "Ohio"; public static const OKLAHOMA:String = "Oklahoma"; public static const OREGON:String = "Oregon"; public static const PENNSYLVANIA:String = "Pennsylvania"; public static const RHODE_ISLAND:String = "Rhode Island"; public static const SOUTH_CAROLINA:String = "South Carolina"; public static const SOUTH_DAKOTA:String = "South Dakota"; public static const TENNESSEE:String = "Tennessee"; public static const TEXAS:String = "Texas"; public static const UTAH:String = "Utah"; public static const VERMONT:String = "Vermont"; public static const VIRGINIA:String = "Virginia"; public static const WASHINGTON:String = "Washington"; public static const WEST_VIRGINIA:String = "West Virginia"; public static const WISCONSIN:String = "Wisconsin"; public static const WYOMING:String = "Wyoming"; public function USAStates() {} public static function toArray():Array { var states:Array = new Array(); var classInfo:XML = describeType(USAStates); for each (var v:XML in classInfo..constant) { var name:String = String(v.@name); var value:String = USAStates[name]; states.push(value); } states.sort(); return states; } } }
Now I can use my standard enum as an array or dataprovider like this:
<mx:ComboBox id="state" width="250" dataProvider="{USAStates.toArray()}" />
What’s up Ken. Interesting post and a much better solution than what you might typically see; an additional Array of values redundantly mirroring those of the constants, in which case should America happen to acquire another state (like Canada – just kidding) and the Array is not updated then everything would be out of sync. Good example of adhering to the DRY principle.
One suggestion I would make would be to cache the Array so as to avoid the expensive overhead of invoking describeType(); for all subsequent calls.