2 Replies Latest reply on Sep 7, 2010 11:25 PM by mike_morales

    AS3 - can you mix inline code with variables?

    mike_morales Level 2

      Hello all,


      I've got an Actionscript question, hoping someone can help me out.  As the thread topic says, is it possible to mix inline code with variables? I've got an arraycollection instance, and I want to refer to one of its properties with a string variable rather than explicit code.  Below is an example of what I am trying to do:


      var myString:String = 'myTableID';


      var myAC:ArrayCollection = new ArrayCollection();


      var myResult:String = myAC.getItemAt(0).myString; (as opposed to: myAC.getItemAt(0).myTableID)


      in actionscript 2, you could do it using brackets myInstance.[var], but it doesn't seem to work in AS3.  Anyone have an idea?  I have a few situations like the above, if I could find a solution, it'd be great. Any help will be greatly appreciated, thanks in advance!



        • 1. Re: AS3 - can you mix inline code with variables?
          BhaskerChari Level 4

          Hi Mike,


          You can use the Square Bracket notation for acheiving this as shown in the below example...



          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                  backgroundColor="white" width="100%" creationComplete="init()">

                   import mx.collections.ArrayCollection;
                   import mx.controls.Alert;
                      import mx.core.IUIComponent;
                      import mx.graphics.ImageSnapshot;
                      private var myAC:ArrayCollection = new ArrayCollection([{data:1, myTableID:"1001"},{data:3, myTableID:"1002"}]);

                      private function init():void
                       var myString:String = 'myTableID';
                       var myResult:String = myAC.getItemAt(0)[myString];
                       //Alert.show(myAC.getItemAt(0).myTableID + "");



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


          Bhasker Chari

          • 2. Re: AS3 - can you mix inline code with variables?
            mike_morales Level 2

            Ah, thanks Bhasker, I was mistakenly putting in a period before the brackets, once I removed the period, it worked! Thanks!!