12 Replies Latest reply on Jan 23, 2008 1:00 PM by ntsiii

    Dynamically created object

    sulletf Level 1
      Hi

      I did not find any way of accessing a dynamically created object by using notation this[id].

      As the code below shows, only hard coded id objects are recognised by this[id] notation.

      Any way to do it ?

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">

      <mx:Script>
      <![CDATA[

      import mx.controls.*;

      public var l:Label;

      public function init():void {
      l = new Label();
      l.id = 'label1';
      l.text = 'firstname';
      addChild(l);

      }

      public function fred(event:Event):void {
      mytextarea1.text+=this['mytextarea1'].name + '\n';
      mytextarea1.text+=this['label1'].name + '\n';

      }
      ]]>
      </mx:Script>

      <mx:TextArea id="mytextarea1" width="1300" height="200"/>
      <mx:Button click="fred(event)" />

      </mx:Application>
        • 1. Re: Dynamically created object
          chris.huston.t10 Level 3
          Try this:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">

          <mx:Script>
          <![CDATA[

          import mx.controls.*;

          public function init():void {
          var l:Label = new Label();
          l.id = 'label1';
          l.name = 'label1';
          l.text = 'firstname';
          addChild(l);
          }

          public function fred(event:Event):void {
          mytextarea1.text+=this['mytextarea1'].name + '\n';
          mytextarea1.text+=this.getChildByName('label1').name + '\n';

          }
          ]]>
          </mx:Script>

          <mx:TextArea id="mytextarea1" width="300" height="200"/>
          <mx:Button click="fred(event)" />

          </mx:Application>

          Vygo
          • 2. Re: Dynamically created object
            paulfeuer
            your Label variable "l" is still within the scope of your fred method. you can also try:

            mytextarea1.text+=l.name + '\n';

            ./paul
            • 3. Re: Dynamically created object
              sulletf Level 1
              Thanks guys but I think my example is too simple compared to my "real world" current problem ...

              In my project, I dynamically create containers and children. I'd like to reach directly a then dynamically created child by its id, but notation this[id] is not recognised.

              A better example : form f1 (id='form1) contains 2 labels (ids 'form1label1' and 'form1label2') ; f1 is included in form f0.
              Calling this['form1label1'] crashed even though it is a declared id control ! The same example using <mx> tags would not crash...

              And I have gone aroud 10000 articles but never found an answer to this.

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">

              <mx:Script>
              <![CDATA[

              import mx.controls.*;
              import mx.containers.*;

              public function init():void {
              var f0:VBox = new VBox();
              var f1:VBox = new VBox();
              var l:Label;

              f0.id = 'form0';

              f1.id = 'form1';

              l= new Label();
              l.id = 'form1label1';
              l.text = 'mobile';
              f1.addChild(l);
              l = new Label();
              l.id = 'form1label2';
              l.text = 'work';
              f1.addChild(l);

              f0.addChild(f1);

              addChild(f0);

              }

              public function fred(event:Event):void {
              mytextarea1.text+=Label( this['form1label1'] ).text + '\n';

              }
              ]]>
              </mx:Script>

              <mx:TextArea id="mytextarea1" width="1300" height="200"/>
              <mx:Button click="fred(event)" />

              </mx:Application>
              • 4. Re: Dynamically created object
                chris.huston.t10 Level 3
                You can nest the getChildByName method to access your objects.

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
                <mx:Script>
                <![CDATA[
                import mx.controls.*;
                import mx.containers.*;
                public function init():void {
                var f0:VBox = new VBox();
                var f1:VBox = new VBox();
                var l:Label;

                f0.name = 'form0';
                f1.name = 'form1';
                l= new Label();
                l.name = 'form1label1';
                l.text = 'mobile';
                f1.addChild(l);
                l = new Label();
                l.name = 'form1label2';
                l.text = 'work';
                f1.addChild(l);
                f0.addChild(f1);
                addChild(f0);
                }

                public function fred(event:Event):void {
                mytextarea1.text+=Label(VBox(VBox(this.getChildByName('form0')).getChildByName('form1')). getChildByName('form1label1')).text + '\n';
                mytextarea1.text+=Label(VBox(VBox(this.getChildByName('form0')).getChildByName('form1')). getChildByName('form1label2')).text + '\n';
                }
                ]]>
                </mx:Script>

                <mx:TextArea id="mytextarea1" width="1300" height="200"/>
                <mx:Button click="fred(event)" />
                </mx:Application>

                Does this solve your problem? If you need more direct access, you could create an ArrayCollection that would hold pointers to all your dynamically created objects.

                Vygo
                • 5. Re: Dynamically created object
                  sulletf Level 1
                  Well it does not solve my problem as I am in a purely dynamic situation where i just get a control id in AS and try to play with it. It means I have no idea of control's container depth location...

                  I can then use the Arraycollection solution which I had thought of but I was trying a clean way...

                  I am over surprised that AS is not able to this job himself !!

                  What would the explaination be ?

                  Thanks !
                  • 6. Re: Dynamically created object
                    sulletf Level 1
                    By the way, how do you declare an Arraycollection of pointers ?
                    • 7. Re: Dynamically created object
                      dietmar.paulus Level 1
                      just use a normal arraycollection and if you have created an object just call yourArrayCollection.addItem(yourObject).

                      the content of the arraycollection is now a reference to the object not the object itself (i think this is called "call by reference")

                      PS.: Such operations always use references and not the value (only primitive types like boolean, string, int ... use values)
                      • 8. Re: Dynamically created object
                        uniqode
                        I was reading about this in the developers guide the other day. I am not sure if this will address your problem but take a look at the "Accessing the target property" topic on p 91 of developers guide. It would be great if you can post your solution as I will need to do the same pretty soon :)
                        • 9. Re: Dynamically created object
                          ntsiii Level 3
                          You can reference a component by id because the compiler creates an instance variable with that name pointing to the component instance..

                          Adobe advises against using getChildByName() becuae the name property is used internally.

                          Best preactice is to create a dictionary of some sort at instance scope. ArrayCollection is fine, I think I would use an associative array(object) for simplicity in reference.

                          Finally, consider using Repeater to handle your component instantiation. It creates the pointer array for you, among other things

                          Tracy
                          • 10. Re: Dynamically created object
                            sulletf Level 1
                            I had a look to page 91 but it accesses "tag ids", not dynamically created ones.
                            Thanks.
                            • 11. Re: Dynamically created object
                              sulletf Level 1
                              I will use an associative array instead like the folowing :

                              public var array:Array = new Array();
                              ...
                              var o:UIComponent;
                              for each ( blabla) {
                              o = new <some type like Label> ();
                              o.id = <id>;
                              array[<id>] = o;
                              }

                              This should work fine.

                              Thanks to all !
                              • 12. Re: Dynamically created object
                                ntsiii Level 3
                                That is a normal indexed array. To use an associative array:
                                public var oArray:Object= [];
                                ...
                                var uicComp:UIComponent;
                                for each ( blabla) {
                                uicComp = new <some type like Label> ();
                                oArray [ "myId1" ] = uicComp;
                                }

                                the reference the components:
                                var uicComp:UIComponent = oArray [ "myId1" ];

                                This way, you do not have to search the array for the id to return the component reference.

                                Note, extra spaces added around the brackets to prevent the forum from eating them.

                                Tracy