2 Replies Latest reply on Oct 4, 2009 7:48 AM by timo888

    associative arrays with string keys problem


      Judging from the documentation here (http://livedocs.adobe.com/flex/3/html/help.html?content=10_Lists_of_data_4.html)  this should be possible:



      private var _List:Object=new Object();

      _List["foo"] =  new Object();



      But when I try to reference the object stored in the dictionary under key "foo"





      I get this error: ReferenceError: Error #1069: Property foo not found on builtin.as$0.MethodClosure and there is no default value


      Why is this happening? How is what I'm doing any different from


      monitorInfo["aspect ratio"]

      in the documentation example?


        • 1. Re: associative arrays with string keys problem
          Flex harUI Adobe Employee

          Post your code.  You started off talking about Object then mention dictionary and the error you're getting is as if you referenced a function.


          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: associative arrays with string keys problem
            timo888 Level 1

            Her's the quasi-dictionary code.  Problem solved -- see below after code snippet. The Entities class encapsulates a dictionary (sort of, implemented as Object()[x]) containing objects of type Entity accessed via string keys.



            package Search
                public class Entities    
                    public var List:Object=new Object();
                    public function Entities() {
               does the encapsulated Object "pseudo dictionary" contain the key?

               public function containsKey(c:String):Boolean {
                   return !(List[c]==null);
               // initialize entities
                private function InitializeList():void {  
                   List["foo"] = new Entity("ae", "ae", "ash", "00E6", "a") ;



            And I've discovered the cause of the error.


            Inisde a program that has created an instance of class Entities:


            private var myEntities:Entities=new Entities;

            var o:Object =  myEntities.List["foo"];   // invalid, generates error


            Adding the following method to the Entities class avoids the problem:


            public function item(key:String): Entity {
                   return  Entity (List[key]);


            because now, inside the program that has created an instance of class Entities, this approach to getting an Entity object from the "pseudo dictionary"  is possible:

            var e: Entity = myEntities.item("foo");


            This method also works:


            public function item(key:String): Object {
                   return List[key];