Can you have "ByRef" arguments in AS3 functions?

Any idea how to return multiple variables from a function in ActionScript 3?

Anything like VB.NET where you can have the input argument's variable modified (ByRef arguments)?

Sub do (ByRef inout As Integer)
 inout *= 5;
End Sub

Dim num As Integer = 10
Debug.WriteLine (num)        '10
do (num)
Debug.WriteLine (num)        '50

Anything apart from returning an associative array?

return {a:"string 1", b:"string 2"}

Answers


Everything in AS3 is a reference aside from [u]ints. To generalize, everything that inherits Object will be given to the function by a reference.

That being said, the only way I believe you can do it is use a container class like an Array or a String ("5" and do the conversion+math).


Quoting a googled source:

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.

Which led me to look up the canonical source.


It appears that Strings, ints, units, Booleans are passed by Value. I tried this little snippet in Flash and the results were negative:

function func(a:String){
    a="newVal";
}

var b:String = "old";

trace(b)    //  old
func(b);
trace(b)    //  old

So... is String a blacklisted data type too? Boolean too? I mean whats a sure way of telling which types are passed by reference?


It's all by value, if you understand C programming you will be familiar with the concept of pointers.

Think about a pointer as pointing to something in memory, and all variable names "bob from (bob = new person();)" Are essentially pointers that you work with.

Now, when you declare a function, since they are all By Value

function Test(a:Object, b:Object):void {
   a = b;
}

You can think about both "a" and "b" being new pointers, so only within the "Test" function do both "a" and "b" exist and point to something in memory.

So let's use it

var s1:Sprite = null;
var s2:Sprite = new Sprite;
Test(s1,s2);

So the s1 and s2 pointers will ALWAYS point to "null" and "a new Sprite in memory" respectively, unless they are modified as s1 and s2 within their "Scope" <- Please make sure you understand variable scope before even trying to tackle this.

And within the function we now have two new pointers "a" pointing to "null" and "b" pointing to "the same sprite in memory as s2". so Since objects and arrays are essentially collections of pointers and only two new pointers have been created by the function for use "a" and "b" any properties/exposed variables "pointers to data in memory" of "a" or "b" will still be exactly the same as the ones for "s1" and "s2" and are the exact same pointers.

So within the function when "a" gets set to be "b", really all that happens is the "a" pointer now points to the same thing as "b". But "s1" and "s2" still point to what they were pointing to before.

!!!! If this was by reference you would not be able to think of "a" and "b" as new pointers, they would actually be "s1" and "s2" themselves, except you write them out as "a" and "b".


Wrong Wrong Wrong and Wrong.. every Argument is passed by value!!! the fact you can change a property inside the object passed doesn't mean you can change the object itself. try the following code

function Test(a:Object, b:Object):void {
   a = b;
}

function Test2():void {
   var s1:Sprite = null;
   var s2:Sprite = new Sprite;

   Test(s1,s2);
   Trace(s1);
   Trace(s2);
}

and here's the trace result :

null
[object Sprite]

Note the subtle difference between DarthZorG's example and this one from the Flash docs:

function passByRef(objParam:Object):void 
{ 
    objParam.x++; 
    objParam.y++; 
    trace(objParam.x, objParam.y); 
} 
var objVar:Object = {x:10, y:15}; 
trace(objVar.x, objVar.y); // 10 15 
passByRef(objVar); // 11 16 
trace(objVar.x, objVar.y); // 11 16

Point Being: You can't change what the reference is pointing to but you can change the data that the reference is pointing to, so long as that reference is an Object/Array.


Need Your Help

Using C# and XDocument/XElement to parse a Soap Response

c# xml soap parsing

Here is a sample soap response from my SuperDuperService: