1 Reply Latest reply on Jun 18, 2009 10:32 PM by Gregory Lafrance

    how to get object attribute by string attribute name?

    powerlevel500?Impossible

      Hi

       

      I'm trying to get an object attribute from my created object via a string.

       

      For example

       

      var obj:Object = new Object;

      obj.param1 = "hello"

      obj.param2 = "world"

       

      obviously I can get the params by going

       

      str:String = obj.param1;

       

      however I'd like to get tit by a string. e.g:

       

      findingStr:String = "param1"

       

      str:String = obj.[findingStr]; //some syntax to get the attribute out

       

      I know you can detect if the object has the attribute by a string, but can someone please tell me how to reference the attribute by the string?

       

      Thanks!

        • 1. Re: how to get object attribute by string attribute name?
          Gregory Lafrance Level 6

          This seems to do the trick:

           

           

          <?xml version="1.0"?>
          <mx:Application
          
           xmlns:mx="http://www.adobe.com/2006/mxml"creationComplete="init();"
          
          >
          
          <mx:Script>
          <![CDATA[
          
          private function init():void{
          
          var obj:Object = new Object;obj.param1 = 
          
          "hello";obj.param2 = 
          
          "world";
          
          var findingStr:String = "param1"
          var str:String = obj[findingStr]; txt.text = str;
          }
          ]]>
          
          
          </mx:Script>
          
          <mx:TextArea id="txt"/></mx:Application>
          
          
          
          
          
          


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