4 Replies Latest reply on Jun 12, 2008 3:57 AM by flexchief

    for loop problem

    flexchief Level 1
      I know this sounds very simple but I just can't seem to get it to work! I need a label(myLabel) to return everything in my array collection(myArrayCollection) one by one and separate them using 10 spaces.

      When I call generateLabelText my label only contains the last item in my array collection, can someone please help me out?

      Many thanks
      Flexchief

        • 1. Re: for loop problem
          m_hartnett Level 3
          If you are trying to concatenate the previous values of the label you need to include what the current value of the label is plus the new data.

          myLabel.text = myLabel.text + " " + myArrayCollection.getItemAt(i) + " ";


          • 2. Re: for loop problem
            Garyl Woolworth Level 1
            You could also do += as in the following myLabel.text += " " + myArrayCollection.getItemAt(i) + " "; which is the equivalent of saying take everything that already exists and add the following to it.
            • 3. Re: for loop problem
              Gregory Lafrance Level 6
              Yep. This illustrates += in this case:

              <?xml version="1.0"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
              <mx:Script>
              <![CDATA[
              import mx.collections.ArrayCollection;
              private var myArrayCollection:ArrayCollection =
              new ArrayCollection(
              ["Here", "is", "the", "label!"]);

              public function generateLabelText():void{
              for(var i:int = 0; i < myArrayCollection.length; i++){
              myLabel.text = " " + myArrayCollection.getItemAt(i) +
              " ";
              }
              }
              public function generateLabelText2():void{
              for(var i:int = 0; i < myArrayCollection.length; i++){
              myLabel2.text += " " + myArrayCollection.getItemAt(i) +
              " ";
              }
              }
              ]]>
              </mx:Script>
              <mx:Label id="myLabel" creationComplete="generateLabelText()"/>
              <mx:Label id="myLabel2" creationComplete="generateLabelText2()"/>
              </mx:Application>
              • 4. Re: for loop problem
                flexchief Level 1
                Thanks so much for your help guys every suggestion you made worked perfectly!

                You have saved me from tearing my hair out ;-)

                I owe you guys!

                Kindest Regards
                Adam
                (Flexchief)