2 Replies Latest reply on Oct 31, 2008 11:56 PM by jcahill23

    Help with this "for" loop...!

    jcahill23
      Hi. I have saved an ArrayCollection to the desktop. When I read it back into the application I get the problem that the ArrayCollection is now a collection of Objects, not a collection of type "Player" which is the class representing each of the elements in the ArrayCollection. Each Player class has three string properties associated with it.

      I created a function to "type" the elements of the ArrayCollection. It takes the ArrayCollection that has been saved on the desktop as the first argument and the "Player" class as the second argument and should return an ArrayCollection that is typed.

      Trying to figure out why it keeps throwing up an error...and whether i need a second for loop inside the first that passes all the variables from the item objects to the temp object.

      Thanks!

      private function typeCollection (collection:Object, clazz:Class):ArrayCollection

      {
      var arr:ArrayCollection = new ArrayCollection();
      for each (var item:Object in collection) {

      var temp:Object = new clazz()
      temp = item;
      arr.addItem(temp);
      }
      return arr; //return the array collection
      }
        • 1. Re: Help with this "for" loop...!
          Gregory Lafrance Level 6
          What is the error and what is clazz in:
          var temp:Object = new clazz()
          • 2. Re: Help with this "for" loop...!
            jcahill23 Level 1
            Hi.

            The error I get is below....

            ArgumentError: Error #1063: Argument count mismatch on Player(). Expected 3, got 0. at main/typeCollection()[C:\Documents and Settings\User\My Documents\Flex Builder 3\main\src\main.mxml:108]

            Line 108 is the line var temp:Object = new clazz();

            When you ask what is clazz in...clazz represents the second argument of the typeCollection function and the argument that is passed in is the class Player which is a class in an external actionscript file.

            Actually just got it to work. The problem was....i created the Player class' constructor function to accept three arguments (id, firstname, surname)....and so new clazz() should have had 3 arguments....i just changed the code below and it seems to work..

            If there is a better way happy to hear!

            Thanks for responding.


            private function typeCollection (collection:Object, claz:Class):ArrayCollection

            {
            var arr:ArrayCollection = new ArrayCollection();

            var a:String;
            var b:String;
            var c:String;

            for each (var item:Object in collection) {

            var temp:Object = new claz(a,b,c)
            temp = item;
            arr.addItem(temp);
            }