9 Replies Latest reply on Mar 13, 2012 2:05 PM by Zolotoj

    Doc question

    Zolotoj Level 3

      From http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf61c8a-7ff9.html:


      You should declare an instance variable for each dynamically created component and store a reference to the newly created component in it, just as the MXML compiler does when you set an id property for a component instance tag. You can then access your dynamically created components in the same way as those declaratively created in MXML.


      Do you think it’s like this:

      var btn:Button = new Button();

      btn.id = "button";



      if yes, then why this does not work?




        • 1. Re: Doc question
          GordonSmith Level 4

          No. You declared a local variable (inside a function) not an instance variable (inside a class).


          If you want your component to have a dynamically created Button, declare


          public var button1:Button;


          in the class and then inside some class method do


          button1 = new Button();

          button1.label = "Whatever";



          You then simply access the button as 'button1'. If you need to make multiple dynamic components, store them in some kind of Array or Object-used-as-a-map. For example,


          public var myDynamicallyCreatedComponents:Object = {};


          and inside some class method do


          var button:Button = new Button;

          button.label = "Whatever";


          myDynamicallyCreatedComponents["WhateverButton"] = button;


          You then can access this particular button as myDynamicallyCreatedComponents["WhateverButton"].


          Gordon Smith, Adobe

          1 person found this helpful
          • 2. Re: Doc question
            Zolotoj Level 3

            What if I cannot do public?

            • 3. Re: Doc question
              J Clow Level 1

              Then declare a private variable outside of the function, but inside the class.

              • 4. Re: Doc question
                Zolotoj Level 3

                Yes, that's what I have done. Still does not work.

                • 5. Re: Doc question
                  Zolotoj Level 3

                  Here is code:

                  <?xml version="1.0" encoding="utf-8"?>

                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"









                              import mx.collections.ArrayCollection;

                              import mx.controls.Alert;

                              import mx.events.ListEvent;

                              import flash.display.DisplayObject;

                              import flash.display.DisplayObjectContainer;

                              import spark.components.Button;


                              private var btn1:Button;

                              private function init():void


                                  btn1 = new Button();

                                  btn1.id = "btn";









                          <!-- Define model for the credit card data. -->



                      <s:VGroup id="gr1">




                  • 6. Re: Doc question
                    J Clow Level 1

                    I don't get why you can't just trace btn1.id for whatever you're trying to do.

                    • 7. Re: Doc question
                      Zolotoj Level 3

                      What I am showing is that:


                      is crashing.

                      • 8. Re: Doc question
                        J Clow Level 1

                        It's crashing because that is not the way to access such an object. What you're trying to do is instead done with the code below


                        var blah:Button= gr1.getElementAt(0) as Button



                        However, if you do not want to do that, just trace btn1.id .I do not see the sense in doing whatever you are trying to do currently. and besides, a Vgroup is not an associative array.

                        • 9. Re: Doc question
                          Zolotoj Level 3

                          What I am trying to do in reality is to have a function that would be able to access an object using the following code:

                          view[objectid]. It works with design time controls, but not with these added at run time.