1 Reply Latest reply on Mar 4, 2011 3:41 AM by Tanu Jain

    Dealing with arrays and labels

    Astrotsp

      Hey i'm quite new to flex and am running flash builder 4.

       

      I need help with this code and have no idea how close or wrong I am.

       

      The aim is to add 10 random numbers to an array and then display

      the array in a text area box.

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application

       

      xmlns:fx="http://ns.adobe.com/mxml/2009"

      xmlns:s="

      library://ns.adobe.com/flex/spark"

      xmlns:mx="

      library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

       

      <fx:Declarations>

       

      <!-- Place non-visual elements (e.g., services, value objects) here -->

       

      </fx:Declarations>

       

      <s:TextInput x="342" y="169" id="ta_outPut"/>

       

      <fx:script>

      <![CDATA[

      var randNums: Array=[];

      var i: int=0;

      while (i < 10)

      {

      randNums[i]=math.random(1,100);

      i++;

      }

      ta_outPut.text = randNums;

      ]]>

       

      </fx:script>

      </s:Application>

       

        • 1. Re: Dealing with arrays and labels
          Tanu Jain

          In script tag, make a function and put all the code for random numbers generation and populating array inside it.Call this function on creationComplete      event of application.

           

                    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                         xmlns:s="library://ns.adobe.com/flex/spark"
                         xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                         creationComplete="fun()">

           

                       <fx:Script>       
                  <![CDATA[       
                  var randNums:Array=[];       
                  var i:int=0;
                  private function fun()
                  {
                      while (i < 10)       
                      {       
                          randNums[i]= Math.random();       
                          i++;       
                      }       
                      ta_outPut.text = randNums.toString();
                  }
                  ]]>   
              </fx:Script>

           

          randNums is an array which can not be directly assigned to textArea's text. Hence use randNums.toString()

           

           

           

          --

          Tanu