09 April 2007

Declaring global array variables in classes

When declaring a global array public/private variable in a class, make sure you only call new Array() in the constructor like so:

class myClass{

private var __myArray:Array;

function myClass(){
__myArray = new Array();
}

}

NOT like this:

class myClass{

private var __myArray:Array = new Array();

function myClass(){}

}

If you declare array vars as above,__myArray in subsequent instantiations of myClass will contain the data from the first instantiation of myClass. Seem like a bug to me, but at least I now know not to declare variables like this again!