7 Replies Latest reply on Sep 22, 2008 2:15 PM by Newsgroup_User

    passing data to event handler function

    Merlyn_MM
      I am trying to pass value to the event handler function but I am getting the following error. Can someone please tell me what I am doing wrong?

      1067: Implicit coercion of a value of type void to an unrelated type function

      here is the code

      private function myClickListener(myid:int):void{
      Alert.show("The button was clicked");
      }


      public function handleStringResult(event:ResultEvent):void{
      catInfo = event.result as ArrayCollection;
      for each(var o:Object in catInfo){
      var b:Button = new Button();
      b.label = o.FILLCOLOR;
      b.id=o.CATID;
      b.setStyle("fillColors",['#'+o.FILLCOLOR,'#'+o.FILLCOLOR]);
      b.setStyle("color","#FFFFFF");
      b.setStyle("fontFamily","Arial");
      b.setStyle("fontSize",8);
      b.setStyle("textRollOverColor", "red");
      b.addEventListener(MouseEvent.CLICK, myClickListener(o.CATID));
      myvbox.addChild(b);
      }
      }
      ]]>

        • 1. Re: passing data to event handler function
          istrasci Level 1
          I believe for addEventListener, you just put the name of the function, not the signature... So you'd change it to:

          b.addEventListener(MouseEvent.CLICK, myClickListener);

          Try that...
          • 2. Re: passing data to event handler function
            Merlyn_MM Level 1
            I need to pass the value o.CATID to myClickListener, If I just use

            b.addEventListener(MouseEvent.CLICK, myClickListener);

            how can I refer o.CATID in the handler function, I am very new to flex .

            I tried b.addEventListener("click",myClickListener(o.CATID)); and I am getting the same error

            Tanks
            • 3. Re: passing data to event handler function
              Level 7

              "Merlyn MM" <webforumsuser@macromedia.com> wrote in message
              news:gb8sgf$rvm$1@forums.macromedia.com...
              >I need to pass the value o.CATID to myClickListener, If I just use
              >
              > b.addEventListener(MouseEvent.CLICK, myClickListener);
              >
              > how can I refer o.CATID in the handler function, I am very new to flex .
              >
              > I tried b.addEventListener("click",myClickListener(o.CATID)); and I am
              > getting
              > the same error

              You can't do that in Actionscript. If you want to pass any parameter but
              the event itself, you have to use MXML.

              Can't you change your event handler to get the required information from the
              event object it gets?


              • 4. Re: passing data to event handler function
                Merlyn_MM Level 1
                Thanks for taking time to help me.

                I am dynamically creating the buttons and onclick of each button I am trying to make a webservice call to get the sub-categories for that categoryID. Thats what I am supposed to do and since this is my first flex project I am not sure how to achieve this.

                • 5. Re: passing data to event handler function
                  Level 7

                  "Merlyn MM" <webforumsuser@macromedia.com> wrote in message
                  news:gb8u0k$at$1@forums.macromedia.com...
                  > Thanks for taking time to help me.
                  >
                  > I am dynamically creating the buttons and onclick of each button I am
                  > trying
                  > to make a webservice call to get the sub-categories for that categoryID.
                  > Thats
                  > what I am supposed to do and since this is my first flex project I am not
                  > sure
                  > how to achieve this.

                  If you'd like to post your code I'll take a look.


                  • 6. Re: passing data to event handler function
                    Merlyn_MM Level 1
                    Sure, Here is the code. I am very new to flex and if my code doesn't make sense then please let me know the correct way to do this. I really appreciate you taking time!

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF"
                    initialize="myService.getcategories.send()">
                    <mx:WebService id="myService"
                    useProxy="false"
                    wsdl=" http://devsite/rc/category.cfc?wsdl"
                    showBusyCursor="true">
                    <mx:operation name="getcategories" result="handleStringResult(event)" fault="Alert.show(event.fault.message)"/>
                    <mx:operation name="getsubcounties" result="handleStringResult(event)" fault="Alert.show(event.fault.message)"/>
                    <mx:request>CATID</mx:request> //need to pass category ID here
                    </mx:operation>
                    </mx:WebService>

                    <mx:Script>
                    <![CDATA[
                    import mx.collections.ArrayCollection;
                    import mx.rpc.events.ResultEvent;
                    import mx.rpc.events.FaultEvent;
                    import mx.controls.Alert;
                    import mx.controls.Button;

                    [Bindable]
                    private var catInfo:ArrayCollection;

                    private function myClickListener(myId:String):void{
                    myService.getcounties.send() // I need to send catid here to the webservice
                    }


                    public function handleStringResult(event:ResultEvent):void{
                    catInfo = event.result as ArrayCollection;
                    for each(var o:Object in catInfo){
                    var b:Button = new Button();
                    b.label = o.FILLCOLOR;
                    b.id='btn'+o.CATID;
                    b.setStyle("fillColors",['#'+o.FILLCOLOR,'#'+o.FILLCOLOR]);
                    b.setStyle("color","#FFFFFF");
                    b.setStyle("fontFamily","Arial");
                    b.setStyle("fontSize",8);
                    b.setStyle("textRollOverColor", "red");
                    b.addEventListener("click", myClickListener);
                    myvbox.addChild(b);
                    }
                    }
                    ]]>
                    </mx:Script>
                    • 7. Re: passing data to event handler function
                      Level 7

                      "Merlyn MM" <webforumsuser@macromedia.com> wrote in message
                      news:gb901m$2qd$1@forums.macromedia.com...
                      > Sure, Here is the code. I am very new to flex and if my code doesn't make
                      > sense
                      > then please let me know the correct way to do this. I really appreciate
                      > you
                      > taking time!
                      >
                      > <?xml version="1.0" encoding="utf-8"?>
                      > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                      > layout="absolute"
                      > backgroundColor="#FFFFFF"
                      > initialize="myService.getcategories.send()">
                      > <mx:WebService id="myService"
                      > useProxy="false"
                      > wsdl=" http://devsite/rc/category.cfc?wsdl"
                      > showBusyCursor="true">
                      > <mx:operation name="getcategories"
                      > result="handleStringResult(event)"
                      > fault="Alert.show(event.fault.message)"/>
                      > <mx:operation name="getsubcounties" result="handleStringResult(event)"
                      > fault="Alert.show(event.fault.message)"/>
                      > <mx:request>CATID</mx:request> //need to pass category ID here
                      > </mx:operation>
                      > </mx:WebService>
                      >
                      > <mx:Script>
                      > <![CDATA[
                      > import mx.collections.ArrayCollection;
                      > import mx.rpc.events.ResultEvent;
                      > import mx.rpc.events.FaultEvent;
                      > import mx.controls.Alert;
                      > import mx.controls.Button;
                      >
                      > [Bindable]
                      > private var catInfo:ArrayCollection;
                      >
                      > private function myClickListener(myId:String):void{
                      >
                      > myService.getcounties.send() // I need to send catid here to the
                      > webservice
                      > }
                      >
                      >
                      > public function handleStringResult(event:ResultEvent):void{
                      > catInfo = event.result as ArrayCollection;
                      > for each(var o:Object in catInfo){
                      > var b:Button = new Button();
                      > b.label = o.FILLCOLOR;
                      > b.id='btn'+o.CATID;
                      >
                      > b.setStyle("fillColors",['#'+o.FILLCOLOR,'#'+o.FILLCOLOR]);
                      > b.setStyle("color","#FFFFFF");
                      > b.setStyle("fontFamily","Arial");
                      > b.setStyle("fontSize",8);
                      > b.setStyle("textRollOverColor", "red");
                      > b.addEventListener("click", myClickListener);
                      > myvbox.addChild(b);
                      > }
                      > }
                      > ]]>
                      > </mx:Script>

                      I'd change it to this:

                      private function myClickListener(e:Event):void{

                      //I don't see where you're using the ID, but to get it,
                      //use this code:
                      var myID = e.currentTarget.data;

                      myService.getcounties.send() // I need to send catid here to the
                      webservice
                      }


                      public function handleStringResult(event:ResultEvent):void{
                      catInfo = event.result as ArrayCollection;
                      for each(var o:Object in catInfo){
                      var b:Button = new Button();
                      b.label = o.FILLCOLOR;
                      //note that you couldn't have used your ID
                      //as a "handle" to anything, so I replaced
                      //that logic with something you _could_ use
                      b.data=o.CATID;

                      b.setStyle("fillColors",['#'+o.FILLCOLOR,'#'+o.FILLCOLOR]);
                      b.setStyle("color","#FFFFFF");
                      b.setStyle("fontFamily","Arial");
                      b.setStyle("fontSize",8);
                      b.setStyle("textRollOverColor", "red");
                      b.addEventListener("click", myClickListener);
                      myvbox.addChild(b);
                      }
                      }

                      You could also use a Repeater and set this up in MXML.

                      HTH;

                      Amy