7 Replies Latest reply on Aug 28, 2009 2:20 AM by Barna Biro

    Pass two parameters to function

    mfriasv

      Hi,

       

      How to pass more than one parameter to a function? In the below example the user selects a radio button, fills in a TextInput and the clicks on a button to submit both parameters to a function. How to tell the function that the event contains two parameters, ie the value of the radio button and the one from the TextInput?

       

          <mx:RadioButton id="png" label="png" />
          <mx:RadioButton id="png8" label="png8" />
          <mx:HBox>
              <mx:Text>
                  <mx:text>Enter the image size</mx:text>
              </mx:Text>
              <mx:TextInput width="30" id="size" text="1024"></mx:TextInput>
          </mx:HBox>
         
         <mx:Button label="Snapshot" click="clickHandler(event)" />

       

      Thanks,

      Manolo

        • 1. Re: Pass two parameters to function
          Andrew Rosewarn Level 3

          Hi

           

          So if I understand you you have a function to do something when this button is clicked.  You'll need to aquire the values on the radio and textinput so the handler from the button click would look like;

           

          private function clickHandler(event:MouseEvent):void {

               // Then you'd have to aquire the values from the two controls

           

               var radio:RadioButton;

           

              // decide which radio button is selected and then store a reference to it in radio variable (cos I dont't know what data you want from it)

               if (png.selected) {

                    radio = png;

               } else {

                    radio = png8;

               }

           

               // Then pass the two parameters to a function you have which accepts two parameters.

               myFunction(textField.text, radio);

          }

           

          Your function to accept these should look something like

           

          function (text:String, radioSelected:RadioButton):void {

           

          }

           

          Theres a few ways to do it thats just one.  Does this help, or have I misunderstood you?

           

          Andrew

          • 2. Re: Pass two parameters to function
            Andrew Rosewarn Level 3

            Ps you'll need to give your textfield and id so you can reference it.

            • 3. Re: Pass two parameters to function
              Barna Biro Level 3

              First of all, it's not mandatory to assign the click handler as clickHandler(event); if you want, you can send an event object or not. If you want, you can define as many parameters as you want and pass in as many data as you want:

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application 
                  xmlns:mx        ="http://www.adobe.com/2006/mxml" 
                  layout          ="vertical">
                  
                  <mx:Script>
                      <![CDATA[
                          import mx.controls.Alert;
                          
                          private function clickHandler(event:MouseEvent, value1:String, value2:Boolean):void
                          {
                              Alert.show(event.currentTarget + " -- " + value1 + " -- " + value2);
                          }
                          
                      ]]>
                  </mx:Script>
                  
                  <mx:TextInput id="ti1" text="Type text here" width="200"/>
                  <mx:CheckBox id="cb1" label="Check it or not"/>
                  <mx:Button label="Click" click="clickHandler(event, ti1.text, cb1.selected);"/>
                  
              </mx:Application>
              

               

              Although this is functional, from a structural point of view is extremly ugly. Since you are a beginner, this solution is better than nothing. A slightly better solution is what Andrew posted but it can get much much better than that.

               

              Good luck.

              1 person found this helpful
              • 4. Re: Pass two parameters to function
                mfriasv Level 1

                Hi Andrew and Barna,

                 

                thanks for your answers! I got the idea but I still would like to ask Barna what the best solution form an experienced developer point of view woul be? You said your solution is structurally ugly so what's the optimal?

                 

                Regards,

                Manolo

                • 5. Re: Pass two parameters to function
                  Barna Biro Level 3

                  Well, I'm guessing that you'll be having even more options from which the user can choose from ( or even have multiple panels/components with options that are used to activa/deactivate or just set some settings/features ), then a much more flexible and reusable solution would be:

                   

                  ( The following is a really brief explanation ):

                   

                  1. Break these options down into smaller pieces/components ( modules ).
                  2. Define and interface that will be implemented by all components that have this "activate/dectivate/set settings feature" ( components that will be used to save or set settings ). The interface would define methods like "saveSettings" or "setSettings" ( so that you can save the settings from a component or initialize the component with some settings ).
                  3. The settings component itself shouldn't really have an OK or SEND button, these buttons should be implemented/added into the "main settings container" ( a container that would hold your smaller settings modules ). Once this SEND/SAVE/OK button is pressed, you should loop trough all the settings modules ( that implement your settings interface ) and call the "saveSettings" method that would return the settings for the certain component ( it would be even nicer is you add in a settings object to this method so that the method won't create a new settings object and return it each time, but instead, it would fill out an existing one ).
                  4. Once the settings are returned, you can decide to do whatever you want with the gathered information.

                   

                  Taking in consideration that you are a beginner, I'd suggest you stick to the solutions that were posted previously. I'm not sure if you understand the approach I have just posted but in case you don't, then don't worry, in time ( once you get more experienced ), you'll understand it.

                   

                  In case you are not planning on having multiple settings and forms and who knows what then just stick to the simple solution. Although the flexibility and reuse that could be achieved with the approach I have just stated wouldn't hurt, that doesn't mean that everything must be overcomplicated, especially if the final result is not planned to be reusable or flexible ( if you know that you'll only have those settings, they will never change then there's no real reason why a more complex mechanism of saving and setting settings should be implemented - although, it wouldn't hurt... who knows what the future might bring ).

                   

                  Good luck.

                  • 6. Re: Pass two parameters to function
                    flex2008 Level 3

                    Another way would be:

                     

                    1)create a custom event and declare properties to hold your data.
                    2)In the click handler dispatch the event after setting the custom event properties with the required data.
                    3)handle the custom event

                    • 7. Re: Pass two parameters to function
                      Barna Biro Level 3

                      flex2008 wrote:

                       

                      Another way would be:

                       

                      1)create a custom event and declare properties to hold your data.
                      2)In the click handler dispatch the event after setting the custom event properties with the required data.
                      3)handle the custom event

                       

                      Yeah, this would also be a solution if the settings would be broken down into smaller pieces and each component should dispatch a custom event once it's OK/SAVE button is pressed. The parent or the application can then catch this event and react to it. But these settings are not broken down into smaller pieces then there's really no point in dispatching custom events. Another drawback would be that each component should have that OK/SAVE button that should dispatch an event.

                       

                      Ok, it's not really a drawback but it might not fit each situation and it could get in the way quite many times ( especially if you want to start grouping settings at a moment - building more complex settings panels that are built by composing N components ).

                       

                      So, the solution one would choose should depend on "the goal of the application".