Skip to main content
Inspiring
December 28, 2007
Question

passing parameter as reference

  • December 28, 2007
  • 5 replies
  • 396 views
Hey everybody, do you know if is it possible to pass a parameter to a
class method as a reference and not as a copy? I try to explain, I'm
trying to convert a c++ steering behavior library from c++ to as3, in
c++ you can pass a parameter as reference using & as example if you
call a method getSum(a,b,&result) then in the getSum function the
'result' property of the calling class was set to the sum of a + b. I
hope you understand what I mean.
There is a way to do this in as3?
thank you
This topic has been closed for replies.

5 replies

kglad
Community Expert
Community Expert
December 30, 2007
you're welcome.
Inspiring
December 30, 2007
Kglad, I have made some expirements, as I thinked variable are passed
as reference only if they aren't primitive data type like Number. I
found a way to pass this limitation passing the reference of the
calling class and the name of the property.
thank you for your answer to my question
Rob

This is the test code


// wrong! the trace return 0
package {
public class A {
public var _sum:Number;

public function A(){
_sum=0;
}
}
}
package {
public class B {
public static function
sum(num1:Number,num2:Number,_result:Number):Boolean{
_result=num1+num2;
return true;
}
}
}

package {
import flash.display.MovieClip;
//
public class Test extends MovieClip{
//
public function Test():void {
var a:A = new A();
B.sum(5,4,a._sum);
trace(a._sum); // return 0
}
}
}

// correct! the trace return 9
package {
public class A {
public var _sum:Number;

public function A(){
_sum=0;
}
}
}
package {
public class B {
public static function
sum(num1:Number,num2:Number,resultObj,varName):Boolean{
resultObj[varName]=num1+num2;
return true;
}
}
}

package {
import flash.display.MovieClip;
//
public class Test extends MovieClip{
//
public function Test():void {
var a:A = new A();
B.sum(5,4,a,"_sum"); // return 9
trace(a._sum);
}
}
}
kglad
Community Expert
Community Expert
December 29, 2007
if you're using a static method, you don't even need to pass an object. so, that code (if it were cleaned) would work.
Inspiring
December 28, 2007
On Fri, 28 Dec 2007 15:45:38 +0000 (UTC), "kglad"
<webforumsuser@macromedia.com> wrote:

>you can pass objects to class methods.

do you mean just like this example?

package {
import B;
class A {
public var dist:Number;

public function A(){
var done:Boolean = B.calcDist(a,b,this.dist);
}
}
}

package {
class B {

public static function calcDist(a:Vector2D,
b:Vector2D, result:Number):Boolen {
result=a.getSum(b);
return true;
}
}
}

Ok I will try
Thanks
kglad
Community Expert
Community Expert
December 28, 2007
you can pass objects to class methods.