Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}
like image 770
Robin Rodricks Avatar asked Dec 02 '08 22:12

Robin Rodricks


Video Answer


3 Answers

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.

like image 141
dkretz Avatar answered Sep 20 '22 13:09

dkretz


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?

like image 21
Robin Rodricks Avatar answered Sep 19 '22 13:09

Robin Rodricks


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).

like image 43
LiraNuna Avatar answered Sep 21 '22 13:09

LiraNuna