4 Replies Latest reply on Mar 22, 2007 8:46 AM by maguskrool

    Best way to insert data in custom class instances

    maguskrool Level 1
      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.

        • 1. Re: Best way to insert data in custom class instances
          Peter Lorent Level 2
          >> 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.

          • 2. Best way to insert data in custom class instances
            maguskrool Level 1
            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[j].setProperty1 (aProp1[j]);
            }

            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.
            • 3. Best way to insert data in custom class instances
              Peter Lorent Level 2
              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

              • 4. Re: Best way to insert data in custom class instances
                maguskrool Level 1
                Thank you, I'm a bit more enlightened now.