4 Replies Latest reply on Apr 21, 2010 8:54 PM by David_F57

    Referring a dynamic object

    mxvx Level 1



      I have created an object as follows

      var obj:Object;

      for(int i=0;i<10;i++) {

           obj = new Object();

           obj.id = "obj" + i;



      Is there anyway to access the object as obj1, obj2.


      for(int i=0;i<10;i++) {

           var obj:Object = XXXXXX("obj" + i)XXXXXX  -->> How can I get the object object here





        • 1. Re: Referring a dynamic object
          GordonSmith Level 4

          Your code isn't doing what you think it's doing. You've declared a single var of type Object, which can store a reference to only one Object. So the first 9 Objects that you create are being lost because the reference to each new one is replacing the last one when you set obj. That leaves the previous instances with no references to them, and they get garbage collected.


          You probably want to be using an Array or Vector.<Object>.


          Gordon Smith

          Adobe Flex SDK Team

          • 2. Re: Referring a dynamic object
            mxvx Level 1



            I am setting the id attribute, so won't it create a reference to the object which has been created.


            Ok my question was completely different from that.


            let us assume the following code


            var obj:Object = new Object();

            obj.id = "obj1";


            Would it be possible to use the id attribute (obj1)  to refer the object instead of the actual reference (obj)?




            • 3. Re: Referring a dynamic object
              ShankarFlex3 Level 2



              Gordon is right, if you had the Array or Vector you could retrieve the object with id attribute.





              • 4. Re: Referring a dynamic object
                David_F57 Level 5



                you should do something like this


                var myObj:arrayCollection = new arrayCollection();


                for (var i:Number = 0; i < 10; i++)


                   var obj:Object  = {

                                                  id: i,

                                                  inf:"A string of info"





                now any time you want object 1 its simply myObj[1], if you need to copy a property from the myObj[1] its myObj[1].inf.


                So you can place values you want in the object and refer to them easily.