5 Replies Latest reply on Aug 1, 2006 6:37 PM by isml

    Pass Param in Constructor

    isml
      Custom Component can be written in MXML or AS. If I want to pass Param to Componet written in AS, I could declare the Constructor with Params.But how should i do if it is written in MXML?

      Thanks in advance
        • 1. Pass Param in Constructor
          ddanone
          When a mxml component is created, Flex converts the mxml into an AS class.
          Take a look on online manual " Creating and Extending Flex 2 Components"

          " Defining properties and methods in MXML components

          You can define methods for your MXML components in ActionScript, and properties in ActionScript or MXML. The Flex compiler converts the MXML component into an ActionScript class, so there is no performance difference between defining a property in MXML and defining it in ActionScript.
          "

          I've dont tested it, but try to create a function with the same name of the mxml component file like a constructor, but i'm afraid that will not works.


          • 2. Re: Pass Param in Constructor
            peterent Level 2
            If you have a custom component, written as an MXML file (say it is MyComponent), then just do:

            var myc:MyComponent = new MyComponent();

            or if the component is in another package:

            import some.package.MyComponent;
            var myc:MyComponent = new MyComponent();

            Make sure you add it to a container or else it will not show up:

            someHBox.addChild( myc );
            • 3. Re: Pass Param in Constructor
              isml Level 1
              Thanks for your reply.
              Suppose i have a custom componet, and I wanna pass a parameter when i new it. If it is written in AS, i can do like this:
              package
              {
              import mx.contains.Canvas;
              public class MyComponet extends Canvas
              {
              private var m_strName:String;
              public function MyComponet(strName:String)
              {
              m_strName = strName;
              }
              }
              }

              But if it is written in MXML, named as MyComponent.mxml
              <mx:Canvas>
              <mx:Script>
              <![CDATA[

              private var m_strName:String;
              public function MyComponet(strName:String):void
              {
              m_strName = strName;
              }

              ]]>
              </mx:Script>
              </mx:Canvas>

              Here i define a function with the same name of MXML file. But i could not compile it and received the following error:

              Multiple constructor definitions found.Constructor may not be defined in <Script/> code.

              It seems that the Complier would not allow me to custom my own Constructor. It define a Constructor for me that i can not modify or override.
              • 4. Re: Pass Param in Constructor
                peterent Level 2
                It is not possible to have multiple constructor functions in ActionScript/JavaScript. If you need to set values at construction time you will have to write an initialization function and call it after instantiating your object or write your component in ActionScript and make the constructor have parameters.
                • 5. Re: Pass Param in Constructor
                  isml Level 1
                  thanks a lot