Skip to main content
Inspiring
October 3, 2007
Answered

change string to variable (sort of)

  • October 3, 2007
  • 2 replies
  • 422 views
Hi. I am making a game in flash which requires lots of similar functions, such as

function getSomeNumber():Number{
return number;
}

I was wondering if i could generalize this by doing the following. take a parameter of string defining the name of the variable. i then want to change this string to the name of the variable so that i can output the value of the variable. a bit like this

function getSomeNumber(nameOfVar:String):Number{
//somehow use string to find out variable name and return value of variable
return unknown;
}

hope this makes sense.

trigger2160
This topic has been closed for replies.
Correct answer trigger2160
actually cl, you're creating two variable references with that code:

this[nameOfVar] and getSomeNumber().

it's probably clearer if you do something like:


clbeech's code was what i was looking 4. Not sure y I need all that extra code kglad but thanks for the help anyway.

2 replies

clbeech
Inspiring
October 5, 2007
@kglad: yeah, I see what you're saying there kg, but I think he's just wanting a return from a previously created list of variable references. I'm not sure why he wouldn't call them directly, but I think it has something to do with the user input from his system being in the form of a String, so he wasn't able to access the value of the var named similarly ... but I follow you, and I guess I should have even simplified the return as ...

return this[nameOfVar];

... rather than setting up the extra reference in the function, but your method is the way to go for constructing a new variable from an input String and value, and I love the wildcard use here, didn't know that would work, cool.

OH, but hey (@trigger also) if the variable already exists, one should be able to reference it directly by calling ...

this['something'];

... and if you've got a function which is gathering the input you could say ...

value = this[input];

... if 'input' is in the form of a string, and it should return the value of the variable, without having to call to a function, right? that's what you're saying by 'just use it directly', I got it now :)
Inspiring
October 4, 2007
FIrst you need to convert the String to Number
Here the Code is
var str:String = "123";
getSomeNumber(str);
function getSomeNumber(nameOfVar:String):Number {
// here you need to convert the string to number bcoz ur return type is Number
var num:Number = Number(nameOfVar);
return num;
}

~~~SK
Inspiring
October 5, 2007
Im sorry but thats not what i am after. i do not want the string changed to a number. i want it changed to a variable name.

var someThing:Number = 20;

I have a variable as above. i want the string changed to the name of the variable (someThing) so that the variable can output its number. Hope that is clear.
clbeech
Inspiring
October 5, 2007
OK, you should be able to do this, if the variable resides in the root timeline, else point to the mc containing the var: