Skip to main content
Inspiring
May 23, 2009
Question

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

  • May 23, 2009
  • 2 replies
  • 463 views

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"

}

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
May 23, 2009

From what I can see, you might almost have it, though I'm wondering what the "." are supposed to be doing...

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

Can you write that out is though it were not an attempt to use a string?  I'm thinking you might instead mean something like...

mySelf[myclassvar] = this[mystring].text;

McFriscoAuthor
Inspiring
May 23, 2009

OK, that worked!  Its a groovy thing.

So, to make my keyListener work, I merely had to change  the syntax from

this[my_array[1].text])  

to

this[my_array[1]]

Thanks Ned!  You've made my day1

Ned Murphy
Legend
May 23, 2009

You're welcome... Think of the brackets as including/implying a "." ahead of them, but a "." inside them isn't seen as an object divider.

just as an added bit of info... If you ever need to string string evaluations, say for something like...

this.movieclip1.movieclipA.property = x;

where 1 and A would be some variable, then it would look like...

this["movieclip"+var1]["movieclip"+varA].property = x;

Ned Murphy
Legend
May 23, 2009

I might take the time to look thru and try to find where you're attempting it, but using a string to identify an object uses an array (or bracket) notation:

this["string"+indexValue].x = 200;