4 Replies Latest reply on Aug 11, 2009 7:34 AM by Jerry62712

    How Do I: initialize an array

    Jerry62712 Level 1

      I want to eliminate some duplication of assignments.  One of those involves the three places I set some strings and arrays.  The first is in the tag.  The second is in the script where a binding is made to the tag.  The third is in a class.  This is to accommodate different languages.  The Tag and initial setting in the script are in English.  The user can switch between that and Spanish.

       

      I was able to set the strings in the script to the class and therefore only have the literals appear twice (tag and class).  But I don't know how to make the array load from the class.  Here is the current code:

       

       [Tag] <mx:FormItem id="standardUtilityAllowanceLabel"
                right="5"
                horizontalAlign="right"
                toolTip="Choose the one that best describes your utility expenses program">
                <mx:ComboBox id="standardUtilityAllowanceData"
                  width="100%"
                  alternatingItemColors="#dfdfdf"
                  dataProvider="{pageDropDownUtility}" 
                  tabIndex="65"/>
               </mx:FormItem>
            </mx:Canvas>
      
      [ActionScript 1 - want to remove/change]
      [Bindable] public var pageDropDownUtility:Array = new Array("None",
           "Receiving or expecting to receive energy benefits from LIHEAP",
           "Billed for AC/Heat",
           "Billed for more than 1 utility excluding AC/Heat",
           "Billed for 1 utility excluding AC/Heat and telephone",
           "Billed for telephone only");
      
      [ActionScript 2 - clear - is their a way to truncate it?]
      // removes current values from arrays on input page
      public function clearArray(arrayToBeCleared:Array):void {
           var j:int = arrayToBeCleared.length;
           var i:int = 0;
           for (i; i < j; i++) {
                arrayToBeCleared.pop();
           }
      }
      
      
      [ActionScript 3 - load array one at a time]
      // input page arrays loaded with selected language
      // class array comes back to an array of array (nested) 
      public function loadArray(arrayToBeLoaded:Array,
           arrayToLoadFrom:Array ):void {
           var j:int = arrayToLoadFrom[0].length;
           var i:int = 0;
           for (i; i < j; i++) {
                arrayToBeLoaded.push(arrayToLoadFrom[0][i]);
           }
      }
      
      
      

       

      What I'm looking for is 1) at the initial binding time to bring in the class, 2) truncating the array (clearing it) and 3) loading at one step instead of one by one.

        • 1. Re: How Do I: initialize an array
          Gregory Lafrance Level 6

          If I understand the three things you want to do, this may help.

           

          1) at the initial binding time to bring in the class

          Not sure what the problem is here.

           

          2) truncating the array (clearing it)

          myArray = new Array();

           

          3) loading at one step instead of one by one.

          public function loadArray(arrayToBeLoaded:Array,
              arrayToLoadFrom:Array, index:uint ):void {
              arrayToBeLoaded = arrayToLoadFrom[index];
          }

           

          If this post answers your question or helps, please mark it as such.

          • 2. Re: How Do I: initialize an array
            Jerry62712 Level 1

            Sorry for being obscure.  The [tag] item has a bindable variable associated with it.  That variable is declared in the ActionScript and its value is set currently by having literals on the right side of the assignment.  Those same literals are values of properties in a class.  Therefore, they are duplicated.  When the user wants them changed, there are two places that need to be changed.

             

            My goal is to have only one place to change.  I tried putting the properties' getter in the ActionScript assignment, but what happened is the various members of the array were concatenated to the first member of the array.

             

            In the class is this:

                private const dropDownUtility:Array = new Array([
                    "None",
                    "Receiving or expecting to receive energy benefits from LIHEAP",
                    "Billed for AC/Heat",
                    "Billed for more than 1 utility excluding AC/Heat",
                    "Billed for 1 utility excluding AC/Heat and telephone",
                    "Billed for telephone only"]);
                public function getDropDownUtility():Array {
                    return dropDownUtility;    }

             

             

            [bindable] actionScriptVariable:Array = getDropDownUtility();

             

            results in:

            actionScriptVariable[0] = None Receiving or expecting ...

             

            when I wanted:

            actionScriptVariable[0] = "None"

            actionScriptVariable[1] = "Receiving or expecting ..."

             

            If the user clicks a button, I use the pop and push methods to clear the array and load the new values.  This involves switching between English and Spanish.

            • 3. Re: How Do I: initialize an array
              Flex harUI Adobe Employee

              (2) creates a new array and reassigns.  Setting myArray.length = 0 clears it.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              1 person found this helpful
              • 4. Re: How Do I: initialize an array
                Jerry62712 Level 1

                Thanks for the reply.  I changed my "clear array" method to use it.  I still need to know how to load the array which is currently done in a routine with a class not using that routine.