Has anyone encountered this before...

If you have a class variable that is of type Array...
DO NOT DO THIS:
private var ListOfThings:Array = new Array();


DO THIS:
private var ListOfThings:Array;
//then in your constructor, do this
ListOfThings = new Array();


If you do the former, Flash will create an Array that is SHARED between all instances of the class!!! (i.e., it makes it STATIC)




I did the former:

//this is in the class declaration section
class Foo
{
private var _pressed_event_message:Array = new Array();


//other stuff omitted

function setPressedEventMessage ( m:String, p:Object ) {
        _pressed_event_message.push( { msg:m, param:p} );
trace("this._pressed_event_message.length: " + _pressed_event_message.length);
}

}


class FooExtender extends Foo {

}



When I create two FooExtender instances...and call the setPressedEventMessage() function, the trace increments as if there these both instances share the same array!!!
var a=new FooExtender();
var b=new FooExtender();


a.setPressedEventMessage( "blah" ); //traces "this._pressed_event_message.length: 1" b.setPressedEventMessage( "blah" ); //traces "this._pressed_event_message.length: 2" a.setPressedEventMessage( "blah" ); //traces "this._pressed_event_message.length: 3" a.setPressedEventMessage( "blah" ); //traces "this._pressed_event_message.length: 4" b.setPressedEventMessage( "blah" ); //traces "this._pressed_event_message.length: 5" b.setPressedEventMessage( "blah" ); //traces "this._pressed_event_message.length: 6"





When I took the initialization out of the variable declaration section and just did this...
class Foo
{
        private var _pressed_event_message:Array;

        function Foo () {
                _pressed_event_message = new Array();
        }

}

All behaved as expected...


Was I not doing something correctly?
-James







_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to