4 Replies Latest reply on May 23, 2009 3:26 PM by Ned Murphy

    Getting, setting contents of a field, whose name is parsed out of target path

    McFrisco Level 1

      It seems that every scripting language I start using, it all goes back to this problem.  How do I evaluate a string as a variable name or text field name, and get or set the contents of that variable or text field?

       

      Here's my attempt so far:  Where do I go off the rails?  Thanks in advance for your help.

       

       

      var mySelf:Person = new Person ("Enter Your Name", "Enter Your Email", "Enter Your Phone");

       

      //Person is an external class file.  The variables in it are named "_HomName, _HomEmail, _HomPhone."

       

      //I name the on screen text fields "HomName_txt, HomName_txt, HomPhone_txt.)


      var keyListener:Object = new Object ();
      keyListener.onKeyDown = function () {
      if (Key.isDown (Key.TAB)) {
        saveData(parseTarget (Selection.getFocus ()));

       

      }
      }
      Key.addListener (keyListener);


      function parseTarget (atarget) {
      var my_str:String = atarget;
      var my_array:Array = my_str.split (".");
      trace (my_array[1]);

       

       

      return my_array[1];
      }


              function parseName(aname)
              //to delete the _something suffix off of a target hame
      {
        var my_str:String = aname;
      var my_array:Array = my_str.split ("_");
      trace (my_array[0]);
      return my_array[0];
      }

       

               function saveData(mystring)
               //to save data from a field or other onscreen container into class object var
               {
               trace("What's in _HomName? " + mySelf._HomName);  //This returns "Enter Your Name"
      myvar = parseName(mystring);
         
      trace("Name from item " + myvar);

      myclassvar =  "_" + myvar;
      //this["var"+i] = "first"

      this["mySelf."+ myclassvar] = this[mystring + ".text"]


      trace ("whats in the class variable " + myclassvar + "? " + this["mySelf."+ myclassvar])  //this returns "undefined"

      }