all groups > flash actionscript > july 2004 >
You're in the

flash actionscript

group:

Initializing class members can cause troubles.


Initializing class members can cause troubles. KevinMatte
7/30/2004 8:09:30 PM
flash actionscript:
Ok, some pain here. I believe this is another ActionScript 2.0 bug. At least,
it's going to throw a few people for a loop.
If you initialize a class member like this:
class X {
var myArray = new Array();
}
All instances of X will share THE SAME myArray. So when you manipulate one,
you're manipulating all of them.

One must do this instead:
class X {
var myArray;
function X() { myArray = new Array(); }
}


Example: Put this in your classpath and from your first frame, run:
TestArray.test("Hello");

class TestArray
{ // TestArray
var children:Array = new Array();

function TestArray(prefix)
{ // TestArray
// !! Uncomment the following line to make it work.
// children = new Array();

dumpChildren("Before adding to children:");
for (var i=0; i<2; i++)
children.push(prefix + i);
dumpChildren("After adding children");
} // TestArray

function dumpChildren(msg)
{ // dumpChildren
trace(msg);
for (var i=0; i<children.length; i++)
trace(" child: " + children);
} // dumpChildren

static function test(msg)
{ // test
trace("Testing x: " + msg);
var x = new TestArray("x.");
trace("Testing y: " + msg);
var y = new TestArray("y.");
trace("Done.");

x.dumpChildren("x after constructors are done.");
y.dumpChildren("y after constructors are done.");
} // test
} // TestArray

produces:

Testing x: Hello
Before adding to children:
After adding children
child: x.0
child: x.1
Testing y: Hello
Before adding to children:
child: x.0
child: x.1
After adding children
child: x.0
child: x.1
child: y.0
child: y.1
Done.
x after constructors are done.
child: x.0
child: x.1
child: y.0
child: y.1
y after constructors are done.
child: x.0
child: x.1
child: y.0
child: y.1

Re: Initializing class members can cause troubles. silkpuppet
7/30/2004 9:30:15 PM
You should initialize it in your constructor. When you intialize objects
inline, they are compile-time constants shared by all instances of the class.

See the docs under: Using ActionScript in Flash > Initializing properties
inline

(Excerpt below)


When you initialize arrays inline, only one array is created for all instances
of the class:

class Bar {
var foo:Array = new Array();
}

Re: Initializing class members can cause troubles. jaminto
12/6/2004 7:32:32 AM
may i take a second to rant? who decided this? this is an exact
contradiction to every other programming language. it's also a contradiction
to common sense and ease of use. the keyword 'static' exists in AS 2.0 - why
not just let us declare a member variable static if we WANT it to be. why give
the programmer the option of initializing strings, numbers, and booleans inline
and basically DARING them to try initializing a member variable like: var
a:Array = new Array(); and then sit back and drink a beer while thousands of
programmers waste thousands of hours figuring out why their code doesn't work?
the thing that pisses me off most is that they felt this huge counterintuitive
contradiction deserved only 4 lines at the end of seemingly harmless help
section. it should be written in a black sharpie on the outside of the flash
mx 2004 box! ( <- a slight exaggeration) that is all....
AddThis Social Bookmark Button