as3corelib Tutorial:How to Use DictionaryUtil Class in Flex

folder DictionaryUtil class provides an easy way to access the keys collection and values collection of an existing Dictionary object. However, with the source of DictionaryUtil class, I noticed the difference between “for in” and “for each in”.

Search-256x256 Demo | DownloadDownload Full Project

See the following source code pls:

var myObject:Object = {firstName:”Tara”, age:27, city:”San Francisco”};
for (var prop in myObject) {
    trace(”myObject.”+prop+” = “+myObject[prop]);
/* output:
myObject.firstName = Tara
myObject.age = 27 = San Francisco

var myObject:Object = {firstName:”Tara”, age:27, city:”San Francisco”};
for each (var item in myObject) {
/* output:
San Francisco

However, the “for in” statement just “Iterates over the dynamic properties of an object or elements in an array and executes statement for each property or element.”, “To get a list of fixed properties, use the describeType() function, which is in the flash.utils package.”. From the “Flex Help Document”, we can find the class definition of Dictionary as “public dynamic class Dictionary”. It means that the “key” is defined as a dynamic property of class Dictionary. So, we can get the keys through the “for in” statement.

public static function getKeys(d:Dictionary):Array
    var a:Array = new Array();
    for (var key:Object in d)
    return a;



Click “add to dictionary” to add a new item into the defined Dictionary object.


Change the key name to add more items into the object.



1. getKeys()

public static function getKeys(d:Dictionary):Array

Returns an Array of all keys within the specified dictionary.


d:Dictionary The Dictionary instance whose keys will be returned.


Array Array of keys contained within the Dictionary.

2. getValues ()

public static function getValues(d:Dictionary):Array

Returns an Array of all values within the specified dictionary.


d:Dictionary The Dictionary instance whose values will be returned.


Array Array of values contained within the Dictionary.

The following is full source code of DictionaryUtilDemo.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="absolute">
			import mx.collections.ArrayCollection;
			import com.adobe.utils.DictionaryUtil;
			private var dic:Dictionary = new Dictionary();

			private var keys:Array;
			private var values:Array;

			private function doAddToDictionary():void
				if(dicKey.text == "" )
					var msg:String = "key of dictionary cannot be empty";

				dic[dicKey.text] = dicValue.text;

				keys = DictionaryUtil.getKeys(dic);
				values = DictionaryUtil.getValues(dic);

	<mx:VBox width="100%" height="100%">
			<mx:FormItem label="key">
				<mx:TextInput id="dicKey" text="key1" />
			<mx:FormItem label="value">
				<mx:TextInput id="dicValue" text="value1"/>
				<mx:Button label="add to dictionary" click="doAddToDictionary()"/>
		<mx:Text text="keys:{ keys.toString()}"/>
		<mx:Text text="values:{ values.toString()}"/>
Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

4 Responses to “as3corelib Tutorial:How to Use DictionaryUtil Class in Flex”

  1. [...] the art of programming. Here is the summary of the Package com.adobe.utils, include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]

  2. James Sun says:

    Very helpful article about the Dictionary class in AS.

  3. An outstanding share! I’ve just forwarded this onto a co-worker who was doing a little homework on this. And he in fact ordered me breakfast simply because I found it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending the time to discuss this subject here on your blog.

Leave a Reply