开发者

Is there a way, in As3, to modify a class variable within a function, if you have a it passed as a parameter?

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 th开发者_高级运维e "closeWindows" method.

Any help it's highly appreciated. Thanks.


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.


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?


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");
                    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜