Groups | Blog | Home
all groups > flash actionscript > november 2006 >

flash actionscript : Array in object doesn't duplicate upon instantiating



Jean-P.
11/24/2006 2:16:19 PM
I guess I'm victim of my own misunderstanding of OOP but can somebody tell me
why if I create two instances of an object which itself creates another array
object, I end up referencing the same array ?

I've attached some oversimplified code to make things clearer. Why are both
arrays containing the same data ? Aren't they different objects ?




class test
{
public var index:Array = new Array();


public function add(someValue:String):Void
{
this.index.push(someValue);
}

}


And then, here is the code in my .fla file

myTest1 = new test();
myTest1.add('abc');

myTest2 = new test();
myTest2.add('def');


trace(myTest1.index);
trace(myTest2.index);

The result I get is:

abc,def
abc,def


I was expecting:

abc
def
anonymous thing
11/24/2006 2:52:28 PM
the Array is static when your property is initialize outside of your
constructor or a method... to solve your problem try to initialize your array
in your constructor



class test{
// choose only one of this declaration to put in your class
public var index:Array;
public var index:Array = null; // really important to not initialize
array from this point if
// you don't won't a
static property

public function test():Void{
this.index = new Array(); // initialisation
}

public function add(someValue:String):Void{
this.index.push(someValue);
}
}
Jean-P.
11/24/2006 4:38:38 PM
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.
anonymous thing
11/24/2006 5:02:30 PM
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();
//.....
Jean-P.
11/24/2006 7:17:32 PM
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.
anonymous thing
11/24/2006 7:52:50 PM
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...
Jeckyl
11/25/2006 10:12:51 PM
[quoted text, click to view]
No .. it happens on all object reference classes (ie thing sother than
primitive types like numbers booleans and strings)

[quoted text, click to view]

.... That's fine

[quoted text, click to view]

.... but that will have the same probelms
--
Jeckyl

Jeckyl
11/25/2006 10:18:09 PM
[quoted text, click to view]

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

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

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
actual array.

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

Jean-P.
11/27/2006 9:47:53 PM
Jeckl,

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.
Jeckyl
11/28/2006 12:00:00 AM
[quoted text, click to view]

You're welcome.

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
things work.
--
Jeckyl

AddThis Social Bookmark Button