Skip to main content
Inspiring
March 16, 2007
Answered

Best way to insert data in custom class instances

  • March 16, 2007
  • 3 replies
  • 284 views
Hi.

I have created a custom class (mySuperClass) in an .as file, defined the constructor, properties and get/set methods. Now in the main .fla I have to declare a large number of instances of that class and fill in each property. The "correct" method, I guess, would be to:

var super1:mySuperClass = new SuperClass();
super1.setProperty1 ( value1_1);
super1.setProperty2 ( value1_2);
...
super1.setPropertyN ( value1_N);

...
var superM:mySuperClass = new SuperClass();
superM.setProperty1 ( valueM_1);
...
superM.setProperty1 ( valueM_N);

However, with large amounts of variables and information that share the same basic characteristics, I always feel tempted to get it done by way of a for... cycle. In this case I'd probably build arrays with the properties to be filled in.

So what I would like to know is how would you guys do this? Are there other ways to do this?

Thanks in advance.

This topic has been closed for replies.
Correct answer Peter Lorent
Well, the example is based on an object that has a datamodel and that can be any object of course. Now, assuming the data is coming from let's say an xml file, I would use a function like attached code to generate a collection of objects.
Edit: so the StoreItem instances are created on the fly and saved in the collection

3 replies

Inspiring
March 22, 2007
Thank you, I'm a bit more enlightened now.
Inspiring
March 18, 2007
Hi Luigi and thanks for your answer.

The thing is, maybe it's just my inexperience but I don't really see how the StoreItemCollection class will help me save work since to use it I would have to have an already declared StoreItem, and what I needed was to save time declaring the StoreItems in the first place.

Referring to my previous example, when I said I would use arrays with the properties I meant something more like this:

var super1:mySuperClass = new SuperClass();
...
var superM:mySuperClass = new SuperClass();

var aSuper:Array = new Array (super1, super2,...,superM);
var aProp1:Array = new Array (value1_1, value2_1,..., valueM_1);
for (j=0; j < aProp1.length; j++) {
aSuper.setProperty1 (aProp1);
}

This could be done with even in a more abstract way and using less code, but for the sake of simplicity I thought it was best to use this to exemplify what I meant.

So, did I miss something simple or implicit in your answer? Thank you.
Peter LorentCorrect answer
Inspiring
March 18, 2007
Well, the example is based on an object that has a datamodel and that can be any object of course. Now, assuming the data is coming from let's say an xml file, I would use a function like attached code to generate a collection of objects.
Edit: so the StoreItem instances are created on the fly and saved in the collection

Inspiring
March 17, 2007
>> In this case I'd probably build arrays
Correct. And to do that you use another class to hold a collection of items. Attached an example of store items and the collection that holds the store items.