5 Replies Latest reply on Jul 29, 2008 9:12 PM by g-s-b

    Passing value of parameters

    Level 7
      Hi,
      I try to call function which is defined in separate as file, this
      function modify value of the arguments and then I try to dispay modified
      value in main aplication:
      This way doesn't work, why?

      -----------------------------------------------------------
      file functions.as
      ....
      public function ModifyData(data1:String, data2:String, ......):void {
      .....
      data1="HOUSE";
      data2="BIKE";
      .......
      }

      -----------------------------------------------------------
      main.mxml

      <mx:Application .....

      <mx:Script source="functions.as"/>
      <mx:Script>
      <![CDATA[
      ....
      public var data1:String;
      public var data2:String;

      data1 = "TEST1";
      data2 = "TEST2";
      Alert.show(data1); //produce alert "TEST1"
      Alert.show(data2); //produce alert "TEST2"

      private function DoIt():void {

      ModifyData(data1,data2,..........);
      Alert.show(data1); //produce alert "TEST1" !?!?! WHY?
      Alert.show(data2); //produce alert "TEST2" !?!?! WHY?
      }

      .........


      function DoIt I call when I click the button.

      Could anyone explain me why data1 don't change?

      Best regards,
      Mariusz
        • 1. Re: Passing value of parameters
          Level 7

          "Mariusz Topczewski" <mtopczewski@poczta.onet.pl> wrote in message
          news:g6i2jr$64k$1@forums.macromedia.com...
          > Hi,
          > I try to call function which is defined in separate as file, this function
          > modify value of the arguments and then I try to dispay modified value in
          > main aplication:
          > This way doesn't work, why?
          >
          > -----------------------------------------------------------
          > file functions.as
          > ....
          > public function ModifyData(data1:String, data2:String, ......):void {
          > .....
          > data1="HOUSE";
          > data2="BIKE";
          > .......
          > }
          >
          > -----------------------------------------------------------
          > main.mxml
          >
          > <mx:Application .....
          >
          > <mx:Script source="functions.as"/>
          > <mx:Script>
          > <![CDATA[
          > ....
          > public var data1:String;
          > public var data2:String;
          >
          > data1 = "TEST1";
          > data2 = "TEST2";
          > Alert.show(data1); //produce alert "TEST1"
          > Alert.show(data2); //produce alert "TEST2"
          >
          > private function DoIt():void {
          >
          > ModifyData(data1,data2,..........);
          > Alert.show(data1); //produce alert "TEST1" !?!?! WHY?
          > Alert.show(data2); //produce alert "TEST2" !?!?! WHY?
          > }

          Strings are always passed by value, if you pass them, which you didn't.
          If you want to do something that will work by reference, you could do
          something like this:

          dataArray = new Array();
          dataArray[1] = 'TEST1';
          dataArray[2] = 'TEST2';

          private function DoIt():void{
          modifyData(dataArray);
          Alert.show(data[1]);
          Alert.show(data[2]);
          }

          modifyData should look like this:

          private function modifyData(theData:Array):void{
          theData[1] = 'HOUSE';
          theData[2] = 'BIKE';
          }

          However, there's probably a better way to accomplish whatever your ultimate
          goal is. What are you trying to accomplish that this is working toward?


          • 2. Re: Passing value of parameters
            Level 7
            I try populate my menuBar, and data is from php page.
            In main application file I have:

            public var menubarXML:XMLList;
            ....
            MyHTTPService(" http://.......",menubarXML,.......);
            Alert.show(menubarXML); //Displayed data is null!
            menuBarCollection = new XMLListCollection(menubarXML);

            where menuBarCollection is a data provider for menuBar component.



            In separate ".as" file I wrote function :

            public function MyHTTPService(phpPage:String, data:XMLList,
            parameters:Object):void {

            var service:HTTPService;

            function useHttpService(phpPage:String, parameters:Object):void {
            service = new HTTPService();
            service.url = phpPage;
            service.method = "POST";
            service.useProxy = false;
            service.resultFormat = "e4x";
            service.addEventListener("result", httpResult);
            service.send(parameters);
            }

            function httpResult(event:ResultEvent):void {
            data = XMLList(event.result);

            Alert.show(data); //Displayed data from PHP is OK!
            }

            useHttpService(phpPage,parameters);
            }

            Could you give me advice how to properly receive data ?
            • 3. Re: Passing value of parameters
              Level 7

              "Mariusz Topczewski" <mtopczewski@poczta.onet.pl> wrote in message
              news:g6i5qd$99h$1@forums.macromedia.com...
              >I try populate my menuBar, and data is from php page.
              > In main application file I have:
              >
              > public var menubarXML:XMLList;
              > ....
              > MyHTTPService(" http://.......",menubarXML,.......);
              > Alert.show(menubarXML); //Displayed data is null!
              > menuBarCollection = new XMLListCollection(menubarXML);
              >
              > where menuBarCollection is a data provider for menuBar component.
              >
              >
              >
              > In separate ".as" file I wrote function :
              >
              > public function MyHTTPService(phpPage:String, data:XMLList,
              > parameters:Object):void {
              >
              > var service:HTTPService;
              >
              > function useHttpService(phpPage:String, parameters:Object):void {
              > service = new HTTPService();
              > service.url = phpPage;
              > service.method = "POST";
              > service.useProxy = false;
              > service.resultFormat = "e4x";
              > service.addEventListener("result", httpResult);
              > service.send(parameters);
              > }
              >
              > function httpResult(event:ResultEvent):void {
              > data = XMLList(event.result);
              >
              > Alert.show(data); //Displayed data from PHP is OK!
              > }
              >
              > useHttpService(phpPage,parameters);
              > }

              Here is a class that I wrote for a presentation that reads XML from an XML
              file. It is designed to be used in MXML as the source of a data binding. If
              your php returns XML you should be able to just set the path property to the
              url of your PHP file and it should just work for you.
              package magnoliamultimedia


              /*

              This component is made available by Magnolia Multimedia

              http://www.magnoliamultimedia.com

              under a creative commons license

              http://creativecommons.org/licenses/by/3.0/


              You are free:

              to Share - to copy, distribute and transmit the work to Remix - to adapt the
              work

              Under the following conditions:

              Attribution. You must attribute the work in the manner specified by the
              author or

              licensor (but not in any way that suggests that they endorse you or your use
              of the work).


              Attribute this work:

              Leave this comment at the top of the file

              */


              {

              import flash.events.Event;

              import flash.net.URLLoader;

              import flash.net.URLRequest;


              import mx.collections.XMLListCollection;


              [Bindable]

              public class XMLLoader extends XMLListCollection

              {

              private var _request:URLRequest;

              private var _path:String;

              private var _urlLoader:URLLoader = new URLLoader;


              public function XMLLoader(source:XMLList = null)

              {

              super(source);

              }

              public function get path():String{

              return _path;

              }

              public function set path(path:String):void{

              _path = path;

              _request = new URLRequest(_path);

              _urlLoader.addEventListener(Event.COMPLETE, getURLResult, false, 0, true);

              _urlLoader.load(_request);

              }


              private function getURLResult(e:Event):void{

              var resultXML:XML = XML(_urlLoader.data);

              this.source = resultXML.children();

              this.refresh();

              _urlLoader.removeEventListener(Event.COMPLETE, getURLResult);

              }

              }

              }

              To use it, add xmlns:mm="magnoliamultimedia.*" to the Application tag. Then
              set up your mxml tag like this (note Outlook Express may remove some
              quotes):

              <mm:XMLLoader id="dataSource" path=" http://www.yourserver.com" />
              <mx:MenuBar dataProvider="{dataSource}" />

              You could possibly even do something like this, though I haven't tried it:

              <mx:MenuBar>
              <mm:XMLLoader id="dataSource" path=" http://www.yourserver.com" />
              </MenuBar>

              HTH;

              Amy


              • 4. Re: Passing value of parameters
                Level 7
                Amy Blankenship wrote:
                >
                > Here is a class that I wrote for a presentation that reads XML from an XML
                > file. It is designed to be used in MXML as the source of a data binding. If
                > your php returns XML you should be able to just set the path property to the
                > url of your PHP file and it should just work for you.
                > package magnoliamultimedia
                > Amy
                >
                >
                Thanks!!! I found, that I didn't pass arguments in one of function's
                call :-((( I corrected this and it's work. Than you very much for your
                attention. I saved your example for future.

                Mariusz
                • 5. Re: Passing value of parameters
                  g-s-b Level 1
                  Without creating a custom component like XMLLoader, I do it this way: