2 Replies Latest reply on May 15, 2008 12:17 AM by FlexPrashant

    Dictionary

    FlexPrashant Level 1
      Hey is anybody knows about Dictionary class , what is use of this Dictionary in Flex . Is anybody has any example on Dictionary , please do not reply me links of adobe help nothing is there.....................
      I have seen Flex store application but not clear .......... Please help me and give some more specific and technical talk about Dictionary
        • 1. Re: Dictionary
          Gregory Lafrance Level 6
          This LiveDocs article gives info on using the Dictionary class:

          http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Live Docs_Book_Parts&file=10_Lists_of_data_166_3.html

          -----------------------------------------------------------------------
          Associative arrays with object keys
          You can use the Dictionary class to create an associative array that uses objects for keys rather than strings. Such arrays are sometimes called dictionaries, hashes, or maps. For example, consider an application that determines the location of a Sprite object based on its association with a specific container. You can use a Dictionary object to map each Sprite object to a container.

          The following code creates three instances of the Sprite class that serve as keys for the Dictionary object. Each key is assigned a value of either GroupA or GroupB. The values can be of any data type, but in this example both GroupA and GroupB are instances of the Object class. Subsequently, you can access the value associated with each key with the property access ([]) operator, as shown in the following code:

          import flash.display.Sprite;
          import flash.utils.Dictionary;

          var groupMap:Dictionary = new Dictionary();

          // objects to use as keys
          var spr1:Sprite = new Sprite();
          var spr2:Sprite = new Sprite();
          var spr3:Sprite = new Sprite();

          // objects to use as values
          var groupA:Object = new Object();
          var groupB:Object = new Object();

          // Create new key-value pairs in dictionary.
          groupMap[spr1] = groupA;
          groupMap[spr2] = groupB;
          groupMap[spr3] = groupB;

          if (groupMap[spr1] == groupA)
          {
          trace("spr1 is in groupA");
          }
          if (groupMap[spr2] == groupB)
          {
          trace("spr2 is in groupB");
          }
          if (groupMap[spr3] == groupB)
          {
          trace("spr3 is in groupB");
          }

          Iterating with object keys
          You can iterate through the contents of a Dictionary object with either a for..in loop or a for each..in loop. A for..in loop allows you to iterate based on the keys, whereas a for each..in loop allows you to iterate based on the values associated with each key.

          Use the for..in loop for direct access to the object keys of a Dictionary object. You can also access the values of the Dictionary object with the property access ([]) operator. The following code uses the previous example of the groupMap dictionary to show how to iterate through a Dictionary object with the for..in loop:

          for (var key:Object in groupMap)
          {
          trace(key, groupMap[key]);
          }
          /* output:
          [object Sprite] [object Object]
          [object Sprite] [object Object]
          [object Sprite] [object Object]
          */

          Use the for each..in loop for direct access to the values of a Dictionary object. The following code also uses the groupMap dictionary to show how to iterate through a Dictionary object with the for each..in loop:

          for each (var item:Object in groupMap)
          {
          trace(item);
          }
          /* output:
          [object Object]
          [object Object]
          [object Object]
          */

          Object keys and memory management
          Flash Player uses a garbage collection system to recover memory that is no longer used. When an object has no references pointing to it, the object becomes eligible for garbage collection, and the memory is recovered the next time the garbage collection system executes. For example, the following code creates a new object and assigns a reference to the object to the variable myObject:

          var myObject:Object = new Object();

          As long as any reference to the object exists, the garbage collection system will not recover the memory that the object occupies. If the value of myObject is changed such that it points to a different object or is set to the value null, the memory occupied by the original object becomes eligible for garbage collection, but only if there are no other references to the original object.

          If you use myObject as a key in a Dictionary object, you are creating another reference to the original object. For example, the following code creates two references to an object--the myObject variable, and the key in the myMap object:

          import flash.utils.Dictionary;

          var myObject:Object = new Object();
          var myMap:Dictionary = new Dictionary();
          myMap[myObject] = "foo";

          To make the object referenced by myObject eligible for garbage collection, you must remove all references to it. In this case, you must change the value of myObject and delete the myObject key from myMap, as shown in the following code:

          myObject = null;
          delete myMap[myObject];

          Alternatively, you can use the useWeakReference parameter of the Dictionary constructor to make all of the dictionary keys weak references. The garbage collection system ignores weak references, which means that an object that has only weak references is eligible for garbage collection. For example, in the following code, you do not need to delete the myObject key from myMap in order to make the object eligible for garbage collection:

          import flash.utils.Dictionary;

          var myObject:Object = new Object();
          var myMap:Dictionary = new Dictionary(true);
          myMap[myObject] = "foo";
          myObject = null; // Make object eligible for garbage collection.
          • 2. Re: Dictionary
            FlexPrashant Level 1
            Thanks but
            I have written that i do not need adobe help lecture on this . I need a live implementation on the Dictionary Class . I need a example where and how do we use this class .okay , If you have implemented in your code then you must reply .................