12 Replies Latest reply on Oct 14, 2010 6:40 AM by Claudiu Ursica

    Pass a parameter to custom component -  get null

    Astraport2012 Level 1

      Main MXML (part of code):

       

      <s:Application

      xmlns:ns1="*"

      creationComplete="init();>

       

      import MyComponent;

       

      private function init():void {

       

      var myArray:Array=["FFF","TTT","RRR"];

      myComp.width=200;

      myComp.height=200;

      myComp.getArray = myArray;

      myContainer.rawChildren.addChild(myComp);

      }

      <fx:Declarations>

      <ns1:MyComponent id="myComp" x="0" y="0" />

      </fx:Declarations>

       

      custom component:

       

       

      package 
      {
                public var getArray:Array;
      public class MyComponent extends Sprite { trace (getArray);//trace null
      }
      }
        • 1. Re: Pass a parameter to custom component -  get null
          Gregory Lafrance Level 6

          I would not declare MyComponent in MXML, but in ActionScript in the script block.

           

          I believe the AS is executed first, so unless you have setup AS in MyComponent creationComplete event handler, when the AS you have is executed, MyComponent does not yet exist.

           

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex Training and Support Services

          1 person found this helpful
          • 2. Re: Pass a parameter to custom component -  get null
            Astraport2012 Level 1

            Thank you.

            I know this, but need to solve the problem this way.

            • 3. Re: Pass a parameter to custom component -  get null
              Claudiu Ursica-USyc3k

              What is myContainer?

               

              I have a working version with my container as UICOmponent inside main.

               

              public class MyComp extends Sprite

                  {

                      private var _getArray:Array;

                       

                      public function MyComp()

                      {

                      }

               

                      public function get getArray():Array

                      {

                          return _getArray;

                      }

               

                      public function set getArray(value:Array):void

                      {

                          trace("setter ", value);

                          _getArray = value;

                      }

               

                  }

               

               

               

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

               

                             xmlns:s="library://ns.adobe.com/flex/spark"

                             xmlns:mx="library://ns.adobe.com/flex/mx"

                             xmlns:ns1="*"

                             minWidth="955" minHeight="600"

                             creationComplete="init();">

                   

                  <fx:Script>

                      <![CDATA[

                          private function init():void {

                               

                              var myArray:Array=["FFF","TTT","RRR"];

                              myComp.width=200;

                              myComp.height=200;

                              myComp.getArray = myArray;

                              myContainer.addChild(myComp);

                          }

                      ]]>

                  </fx:Script>

                   

                  <fx:Declarations>

                      <ns1:MyComp id="myComp"/>

                  </fx:Declarations>

                   

                  <mx:UIComponent id="myContainer"/>

                   

              </s:Application>

               

               

              Not sure this is what you after though but it traces:

               

              setter  FFF,TTT,RRR

               

              Why do you add to rawChildren?

               

               

              C

              • 4. Re: Pass a parameter to custom component -  get null
                Astraport2012 Level 1

                OK. Then how I can create a custom component in the script block that I can send and receive variables?

                • 5. Re: Pass a parameter to custom component -  get null
                  Astraport2012 Level 1

                  >>Why do you add to rawChildren?

                   

                  Because I need to put my components in a container:

                  <mx:Container x="34" y="163" width="260" height="260" id="cloudContainer" borderStyle="solid" backgroundAlpha="0.0" borderColor="#91EED9" visible="true">

                   

                  </mx:Container>

                   

                  Thank you Claudiu

                  • 6. Re: Pass a parameter to custom component -  get null
                    Claudiu Ursica Level 4

                    cloudContainer.rawChildren.addChild(myComp);

                     

                    replaced the old line with this and works...

                    C

                    • 7. Re: Pass a parameter to custom component -  get null
                      Astraport2012 Level 1

                      Hooray! Everything works fine! You are a genius:)

                      • 8. Re: Pass a parameter to custom component -  get null
                        Astraport2012 Level 1

                        I knew it. It does not matter. Thanks for the tip with getters and setters.

                        • 9. Re: Pass a parameter to custom component -  get null
                          JeffryHouser Level 4

                          I didn't see anyone say this yet, so..

                           

                          Why are you defining the variable in the package, as opposed to the component:

                           

                          Don't do this:

                           

                           

                          package 
                          {
                                    public var getArray:Array;
                          public class MyComponent extends Sprite { trace (getArray);//trace null
                          }
                          }

                           

                          I'm surprised that even works.

                           

                          Do this:

                           

                           

                           

                          package 
                          {
                          public class MyComponent extends Sprite { trace (getArray);//trace null
                                    public var getArray:Array;

                          } }

                           

                          Which defines the variable in the body of the component. I'll also add that the trace statement is not in a method or constructor.  I had no idea that was possible either.  So, something more like this:

                           

                           

                          package 
                          {
                          public class MyComponent extends Sprite { 

                          public function MyComponent():void{
                          trace (getArray);//trace null
                          }

                                    public var getArray:Array;

                          } }

                           

                          In this situation, with the trace in the constructor, I would expect it to return null because no value is set for the Array yet.  To give more details, we'd have to see your where you actually perform the trace.

                           

                          All that said, your code is horribly odd.  Why areyou trying to instantiate the component in the declarations section; then add it as a child on creationComplete?

                          1 person found this helpful
                          • 10. Re: Pass a parameter to custom component -  get null
                            Claudiu Ursica Level 4

                            "All that said, your code is horribly odd."

                             

                            I'd say so, I just took the code as it came tried to work with client's

                            material. I learned this the hard way, but I am trying to keep myself from

                            putting refactoring ideas all the time into peoples head.

                            Maybe they have a reason for writing the code like that.

                             

                             

                            C

                            • 11. Re: Pass a parameter to custom component -  get null
                              Astraport2012 Level 1

                              >>Why areyou trying to instantiate the component in the declarations section; then add it as a child on creationComplete?

                               

                              I do not remember, but only this method gave a positive result.

                              I tried for several hours. Component rather complicated - I gave a piece of code.

                              • 12. Re: Pass a parameter to custom component -  get null
                                Claudiu Ursica Level 4

                                It is better to overrid the createChildren() if you want to add children outside

                                MXML. I don't know your use case so I can't say what I have used instead of that

                                Sprite and the Wrapping container in that way.

                                 

                                C

                                 

                                From: Astraport2012 <forums@adobe.com>

                                 

                                To: Ursica Claudiu <the_braniak@yahoo.com>

                                Sent: Thu, October 14, 2010 3:21:00 PM

                                Subject: Re: Pass a parameter to custom component - get null

                                 

                                 

                                I do not remember, but only this method gave a positive result.

                                I tried for several hours. Component rather complicated - I gave a piece of

                                code.