views:

41

answers:

3

Here is a simple example of what I mean:

private var ex:String="day";

......

closeWindows(ex);
trace(ex);//I would like it to trace "night"

......

//this can be in another class, we assume that the "ex" variable can't be accessed directly
private function closeWindows(context:String):void {
  context = "night"
}

Somehow I would need to pass the reference not the value itself to the "closeWindows" method.

Any help it's highly appreciated. Thanks.

+1  A: 

AS3 doesn't support passing primitives by reference, unfortunately. So you're right, one of the typical solutions is to use some kind of wrapper for the primitive, such as in this question.

eldarerathis
+1  A: 

Probably you are already aware of this, but in Actionscript parameters are always passed by value. So what you want to do is not possible, unless use some ad hoc wrapper (object, array, or whatever).

But can't you just return and reassign the new string?

Juan Pablo Califano
A: 

Thank you both for the answers, in the end it seems that there is no way to pass a variable as reference not as value. However, you can pass a function as a reference, so here is my little get/set hack.

private var ex:String="day";

                    ......

                    closeWindows(setEx);
                    trace(getEx());

                    ......


                    private function getEx():String {
                        return ex;
                    }

                    private function setEx(value:String):String {
                        ex = value;
                    }


                    private function closeWindows(context:Function):void {
                        context.call(null, "night");
                    }
mindnoise