Currently Reading

Image of Derivatives Demystified: A Step-by-Step Guide to Forwards, Futures, Swaps and Options (The Wiley Finance Series)

Image of Working Effectively with Legacy Code (Robert C Martin)

Image of Coders at Work: Reflections on the Craft of Programming

Image of Succeeding with Agile: Software Development Using Scrum (Addison-Wesley Signature)

Recently Read

Image of The Secrets of Consulting: A Guide to Giving and Getting Advice Successfully

Image of iPhone Advanced Projects (Books for Professionals by Professionals)

Image of Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)

Image of Test Driven Development (The Addison-Wesley Signature Series)

Image of UML 2 for Dummies

Image of Beginning iPhone 3 Development: Exploring the iPhone SDK

Image of Refactoring: Improving the Design of Existing Code (Object Technology Series)

Image of Cloud Atlas

Image of Advanced Actionscript 3 with Design Patterns

Image of ActionScript 3.0 Design Patterns: Object Oriented Programming Techniques (Adobe Developer Library)


June 5, 2008

Using describeType( ) and getDefinitionByName( ) to return a Dictionary of public static const

Filed under: ActionScript, Flex — Anthony @ 2:47 pm

Wow that was a big title!
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( );
   }
}

2 Comments »

  1. 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

  2. gold! thx.

    Comment by ash — December 10, 2009 @ 11:10 pm

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .