Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't make static array in my class..

Status
Not open for further replies.

Dustman

Programmer
May 7, 2001
320
US
I have a question class. Each question can have child questions. And each question will have a parent question. My parent variable works fine but it seems that no matter what, my array is static. In other words, each of my Questions are sharing the same Children array. Is this a bug/feature or am I just missing something here?

Question.as
Code:
class Question {
	var QText:String;
	var QValue:String;
	var QID:String;
	var ParentID:String;
	private var Parent:Question;
	private var Children:Array = new Array();
	
	function Question() {
		trace("new question");
	}
	
	function addChild(childQuestion:Question) {
		this.Children.push(childQuestion);
		trace(this.Children.length);
	}
}

-Dustin
Rom 8:28
 
Ahh.. and once again I answer myself. If you actually initalize the variable in the class definition, it becomes static. If you want to declare variables, you should use the constuctor.

I should know better than that.. hmm.. i'll just blame it on the dog..

Anyways, here's the fix:

Question.as
Code:
class Question {
    var QText:String;
    var QValue:String;
    var QID:String;
    var ParentID:String;
    private var Parent:Question;
    private var Children:Array;
    
    function Question() {
        trace("new question");
        this.Children = new Array();
    }
    
    function addChild(childQuestion:Question) {
        this.Children.push(childQuestion);
        trace(this.Children.length);
    }
}

-Dustin
Rom 8:28
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top