• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

Creating class-wide ("package") variables with AS 2.0

Let's say I have an AS2.0 class called myClip, and in my calling script I invoke multiple instances of myClip() objects. Is there any way, within the class itself, to keep track of how many myClip objects have been created?

Currently, I'm doing this in the calling script, by just creating a variable which I increment each time a new myClip is created. But that would cause problems if I destroy a clip and would want to then decrement that value.

It would be so much more convenient if I could create a variable that's "global" across all instances of the class, like the VB "shared" accessor. I believe AS3.0 allows "package" variables which are accessible across the whole package, is that right?

At any rate, is there any way to do this in AS2?

Tom
0
tomaugerdotcom
Asked:
tomaugerdotcom
  • 3
1 Solution
 
emphaticDigitalCommented:
//give the class a 'static variable:
class myclass{
    //static variable declaration
    private static var _instances = 0;

    //public access to variable
    public function instances(){
        return _instances;
    }

    //then in your class constructor, increment this variable
    public function myclass () {
         //increments here for each new object
         _instances++;
    }
}

//access the value like so
trace(myclass.instances);

//should work
0
 
emphaticDigitalCommented:
Hello:

I re-read your question and I noticed a mistake as well. Here is a much better code that should do what you need.

//give the class a 'static variable:
class myclass{
    //static variable declaration
    private static var _instances = 0;

    //public access to variable
    public static function instances(){
        return _instances;
    }
    public static function kill_instance(){
        if(_instances>0){
            _instances--;
        }
    }
       

    //then in your class constructor, increment this variable
    public function myclass () {
         //increments here for each new object
         _instances++;
    }
}

//access the value like so
trace(myclass.instances);

//remove an instance with
myclass.kill_instance();

//should work now, really.
0
 
tomaugerdotcomAuthor Commented:
Good stuff. Thank you! Static vars work in AS2 just like in AS3, as it turns out, except there's no package definition, I guess.
0
 
emphaticDigitalCommented:
Actually they do have packages in AS2, they're just not required the way they are in AS3. In AS3, if you don't put your classes in a package, an unnamed package is created at compile time to hold all unpackaged classes. Glad I could help.

eD
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now