9 Replies Latest reply on Mar 4, 2008 1:37 AM by vEr5e

    Passing by Reference

    vEr5e
      Actionscript doesn't seem to allow passing by reference, does it?
      I have tried a few ways and found out that variables and objects are copied instead of a reference when being pass say from somewhere into a class method or function.

      But suprisingly, when I tried to pass the id of a Flex component into a method(through the method's parameter) of a class that just create to do the test, it seems to be a reference because whatever property I change to the component reflects directly to the same component.

      I don't quite understand why is this so?
        • 1. Re: Passing by Reference
          levancho Level 3
          can you post a small example?
          • 2. Re: Passing by Reference
            VarioPegged Level 2
            Primitive data types (Boolean, int, String, etc.) are passed by value in AS3 and arguments are passed by reference.

            See the Flex Help docs for more info.

            TS
            • 3. Re: Passing by Reference
              levancho Level 3
              quote:

              Originally posted by: VarioPegged
              Primitive data types (Boolean, int, String, etc.) are passed by value in AS3 and arguments are passed by reference.

              See the Flex Help docs for more info.

              TS

              everything is passed by reference in AS3 I dont thinkink AS3 variable can actually store a primitive data tapy(only memory addresse) because, there is no primitive data in AS3 there is primitive data types which are Objects (as well) that behave as primitives) and they use a internal operators to simulate pass by values.
              button line .... it behaves as expected primitive data type Objects are passed by value everything else is by refference.
              • 4. Re: Passing by Reference
                vEr5e Level 1
                in fileone.mxml, I have something like this:
                <mx:Script>
                <[cdata[
                import OutsideClass;

                private var testRef:Object = {"testIndex": "FileOne"};
                [Bindable] private var dp:ArrayCollection = new ArrayCollection(["a", "b", "c"]);

                private function init():void {
                var outsideClass:OutsideClass = OutsideClass();
                outsideClass.changeObject(testRef); //This doesn't affect the testRef at all
                outsideClass.changeTileList(myTileList); //This will change the dataprovider of the tilelist.
                }
                ]]>
                </mx:Script>
                <mx:TileList id="myTileList" dataprovier="{dp}" />


                then inside the file call OutsideClass.as, i have something like...
                package {
                //some imports
                public class OutsideClass {

                public function OutsideClass() {}

                public function changeObject(testRef):void {
                testRef.testIndex = "OutsideClass";
                testRef.newIndex = "NewIndexFromOutsideClass";
                }

                public function changeTileList(tileList):void {
                var dp:ArrayCollection = tileList.dataProvider as ArrayCollection;
                dp.addItem("D");
                dp.refresh();
                }
                }
                }

                I am wondering why the testRef Object isn't passed as a reference but the myTileList TileList instance is passed as a reference since whatever I do in OutsideClass could change the dataprovider directly.
                • 5. Re: Passing by Reference
                  Bryan Dresselhaus
                  If you want to access an object by value, you can use mx.utils.ObjectUtil.copy function to copy an object.
                  • 6. Re: Passing by Reference
                    levancho Level 3
                    just ran simplified version it works as expected :


                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application creationComplete="init()" xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
                    <mx:Script>
                    <![CDATA[
                    import OutsideClass;

                    private var testRef:Object = {"testIndex": "FileOne"};

                    private function init():void {
                    var outsideClass:OutsideClass = new OutsideClass();
                    trace(testRef.testIndex); // output FileOne
                    outsideClass.changeObject(testRef);
                    trace(testRef.testIndex); // output OutsideClass
                    }
                    ]]>
                    </mx:Script>


                    </mx:Application>



                    ------------------


                    package
                    {
                    public class OutsideClass
                    {
                    public function changeObject(testRef):void {
                    testRef.testIndex = "OutsideClass";
                    }

                    }
                    }


                    ----------------------


                    Output :
                    FileOne
                    OutsideClass

                    it does pass by reference as expected, you must have some mistake in e code.

                    • 7. Re: Passing by Reference
                      vEr5e Level 1
                      hmm...that's weird because it doesn't work the last time I tried. I will go and try again and get back here.

                      So am I right to say that all dataypes in Actionscript are passed by reference. Only String will pass as a copy of a new variable. Is this right?
                      • 8. Re: Passing by Reference
                        ntsiii Level 3
                        From the docs: "In ActionScript 3.0, all arguments are passed by reference, because all values are stored as objects. However, objects that belong to the primitive data types, which includes Boolean, Number, int, uint, and String, have special operators that make them behave as if they were passed by value."

                        Tracy
                        • 9. Re: Passing by Reference
                          vEr5e Level 1
                          So it says that Boolean, Number, int, uint, and String behave as if they were passed by value, which also means that we will access it like a value and not a reference. Then if we don't want it to behave this way and want to access it by reference, is there anywhere to do so?

                          Like say in PHP, you can probably have an ampersand to access it by reference.