This content has been marked as final. Show 9 replies
the Array class is static when your property is initialize outside of your constructor or not in a method... to solve your problem try to initialize your array in your constructor
Thanks a lot. I did not know that.
I've initialized variables in this way for quite some time but never ran into the problem because they all were single instance objects. I'll have to clean up some of my old code.
I tried to google this issue but couldn't figure out what to search for... Thanks again.
You must know that this behavior only happen with the Array class. For everything else you can continue like you did before.
So this examples work perfectly...
public var strTest:String = "";
public var objTest:Object = new Object();
That's odd. Is there a reason why this would only happen with the array class ? An array is an object like any other, no ? Is this the way other OO programming languages work ?
Many questions... If you do not have time, don't bother. I'm just curious and you already solved my problem. Thanks again.
Honestly, I don't know!
Like you, I searched and found nothing that could explain this behavior... check this livedoc link : http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001191.html
at the "Francis Cheng said on Jan 18, 2005 at 9:24 AM :" comment.
If you find a clue it would be nice if you can post it...
> You must know that this behavior only happen with the Array class.
No .. it happens on all object reference classes (ie thing sother than
primitive types like numbers booleans and strings)
> everything else you can continue like you did before.
> So this examples work perfectly...
> public var strTest:String = "";
... That's fine
> public var objTest:Object = new Object();
... but that will have the same probelms
> That's odd. Is there a reason why this would only happen with the array
> class ?
It isn't only the array class. Its any object class
Reason is, that in Flash AS, the code on the RHS of the '=' for a var is
evaluated once when the class is defined.
So if you have
var s : String = "1234";
the "1234" is evaluated once (which is fine, its just a constant) and the
result saved and set as the default values for the var for each new
Similarly, if you have
var a : Array = new Array();
the 'new Array()' is evaluated once and the result saved and set as the
default values for the var for each new instance.
As you may already know, if a and b are arrays, and you set a = b; then a
will now refer to the identical array as b does (so changing a changes b and
That means, if you use the above way of defining an array variable (or
similarly for object), then each instance will have a reference to the one
To fix it, just declate
var a : Array;
and in the constructor function have
a = new Array();
that wat the 'new Array()' happens for each new instance of the clase
(because the consutrctor function is run once for each new instance).
Thanks for you clarification. With the background you provided it makes perfect sense. Its just a little counte-intuitive at first because you expect anything in a object to start fresh with each instance.
Thanks to all who helped.
> Thanks for you clarification. With the background you provided it makes
> perfect sense. Its just a little counte-intuitive at first because you
> anything in a object to start fresh with each instance.
NOTE: It really is a bug in the way Macromedia decided to implement classes.
They COULD have generated code that did the creation of members all at
instantiation time. According to the ECMA language standard, that is how it
SHOULD work. Macromedia just used the syntax but made it work differently
.. just like the way they use the same syntax for typed variables but
implement it 'wrong'.
AS3 (from what I understand) corrects these bugs and actually follows the
standard for not only cosmetically syntax, but (just as importantly) how