Ok i know its not something you would usually have to do but I think this is useful anyway when you have a large list of public static constants declared in a class and you want to somehow loop through them without having to place them in an array or dictionary manually. For example i was writing a Translation utility class today and i needed to add this list of supported languages. ;
//Below are all the current language codes supported by the //flash.system.Capabilities.language class :04/06/2008 //But that doesnt mean you cant pass in your own; public static const CZECH : String = 'cs'; public static const DANISH : String = 'da'; public static const DUTCH : String = 'nl'; public static const ENGLISH : String = 'en'; public static const FINNISH : String = 'fi'; public static const FRENCH : String = 'fr'; public static const GERMAN : String = 'de'; public static const HUNGARIAN : String = 'hu'; public static const ITALIAN : String = 'it'; public static const JAPANESE : String = 'ja'; public static const KOREAN : String = 'ko'; public static const NORWEGIAN : String = 'no'; public static const OTHER_UNKNOWN : String = 'xu'; public static const POLISH : String = 'pl'; public static const PORTUGUESE : String = 'pt'; public static const RUSSIAN : String = 'ru'; public static const SIMPLIFIED_CHINESE : String = 'zh-CN'; public static const SPANISH : String = 'es'; public static const SWEDISH : String = 'sv'; public static const TRADITIONAL_CHINESE : String = 'zh-TW'; public static const TURKISH : String = 'tr'; private var _supportedLanguages : Dictionary; private var _availableLanguages : ArrayCollection;
Once I had this list i realized that I would also like to be able to return an ArrayCollection of all the available languages now i could have done this by creating a new ArrayCollection and adding in each language manually as an object but that means i would have to update my code in two places everytime we decided to add more languages. a better solution is to use the describeType and getDefinitionByName( ) methods in the constructor method as follows.
public function TranslationUtility( se : SingletonEnforcer ) { _supportedLanguages = new Dictionary( ); for each( var con : XML in describeType( getDefinitionByName( 'com.utils.TranslationUtility' ) ).child( 'constant' ) ) { _supportedLanguages[ TranslationUtility[ con.@name.toString( )] ] = con.@name.toString( ); } }














I like it! Simple way to loop through constants is going to save me a lot of code duplication. Cheers
Comment by Dave — December 1, 2008 @ 3:19 pm
gold! thx.
Comment by ash — December 10, 2009 @ 11:10 pm