4 Replies Latest reply on Nov 22, 2010 3:44 AM by BhaskerChari

    Dynamically change property names

    Scott S_DW



      I have several datefields and would like to use a loop to get the value of each but can't figure out how to dynamically assign that name from an array:


      ie:---- this works fine


      if(start_date.selectedDate == null){
                          newTraineeValue.start_date = _trainee.start_date;
                      } else{
                      newTraineeValue.start_date = ((start_date.selectedDate.fullYear+"-"+(start_date.selectedDate.month+1)+"-"+start_date.s electedDate.date).toString());




      This throws up an error ---------------


      var myArray:Array = ["start_date","pment1_date", "pment2_date", "pment3_date"];


                      for (var i:String in myArray)
                      if(myArray[i].selectedDate == null){
                          newTraineeValue.myArray[i] = _trainee.myArray[i];
                      } else{
                      newTraineeValue.myArray[i] = ((start_date.selectedDate.fullYear+"-"+(start_date.selectedDate.month+1)+"-"+start_date.s electedDate.date).toString());


      How do I get the value of myArray[i] and use it as the name of the object????  And how do I also use myArray[i] for a property???




        • 1. Re: Dynamically change property names
          BhaskerChari Level 4

          Hi Scott S_DW,


          Is newTraineeValue is an instance of your CustomClass you created..??


          I think you can try this..Instead of dot operator try to use the Square Bracket syntax...


          You cannot use myArray[i].selectedDate .... in this fashion since myArray[i] returns you the string which holds the name of your datefield id's but selectedDate property is available on DateField control but not string....So you cannot use myArray[i].selectedDate. So you should use something like below if your all the DateFields are placed in one container..


          <mx:Canvas id="dateFieldContainer">

               <mx:DateField1 />

               <mx:DateField2 />

               <mx:DateField3 />




          If your all DateFields are not placed in a single container then you need to write a logic so that based on the string id you can return a DateField object by comparing the string...


          for (var i:String in myArray)
                          if((dateFieldContainer.getChildByName(myArray[i]) as DateField).selectedDate == null){
                              newTraineeValue[myArray[i]] = _trainee[myArray[i]];
                          } else{
                          newTraineeValue[myArray[i]] = ((start_date.selectedDate.fullYear+"-"+(start_date.selectedDate.month +1)+"-"+start_date.selectedDate.date).toString());





          • 2. Re: Dynamically change property names
            Scott S_DW Level 1

            Hi Bhasker,


            thanks for the reply.


            Yes - newTraineeValue and _trainee are instances of a custom class.


            I understand what you are saying - but just couldn't work out the correct syntax.



            I placed all of the DateFields into a group container and then changed the first line using the group container id  -   if((editContainer.getChildByName(myArray[i]) as DateField).selectedDate == null){



            I now get the error --- Cannot access a property or method of a null object reference -- on the line --

            if((editContainer.getChildByName(myArray[i]) as DateField).selectedDate == null){



            Any ideas.




            • 3. Re: Dynamically change property names

              use the hasOwnproperty of the array method to check if the property exists or not.


              mark this answered if helpful

              • 4. Re: Dynamically change property names
                BhaskerChari Level 4

                Hi Scott,


                The elements in your Array myArray[i] are the ids of your DateFields right...


                Try to do the below...


                In the below check whether you are getting dateField as not a null Object...And make sure you have correctly placed all the DateFields in the editContainer container. Can you post the code where you placed the DateFields in editContainer.


                var dateField:DateField = editContainer.getChildByName(myArray[i]) as DateField;

                if(dateField != null && dateField.selectedDate == null){


                Try to debug and check the dateField variable..