Skip to main content
somascope
Inspiring
December 20, 2007
Question

Creating dynamically-named variables inside a class

  • December 20, 2007
  • 1 reply
  • 234 views
I'd like to have a class that creates the necessary amount of variables when it's instantiated according to data passed to its constructor. This means that, in the class definition, I cannot declare any of these - they wouldn't exist before the class is used. The situation is just in creating page variables to then set from false to true when completed. I would just need to create internal booleans named page#, and set them to false (page0 = false, page1 = false, etc.).

Of course, when trying this, I get an error "Cannot create property..." - so how is this properly done?
This topic has been closed for replies.

1 reply

Inspiring
December 21, 2007
Declare your class dynamic.

package com.course {
public dynamic class CompletedPages {

public function CompletedPages(myXMLFile:XML){
// create as many page# variables as there are pages in the XML file
for (var i:int=0; i<courseXML.page.length(); i++) {
this["page"+i] = false;
}
}
}