0

I have the following problem:

I have a function which receives multiple variables:

function test( fooValue:String, foobar1:String, foobar2:String, goobar1:String, goobar2:String ) {
//using the values statically
mytext1.text = foobar1;
mytext2.text = foobar2;
mytext3.text = goobar1;
mytext4.text = goobar2;

if ( goobar1 = "problem" ) {
    myProblem.text = this["foo" + fooValue] + this["goo" + fooValue];
}
}
//now here's an example call
test( "bar1","first value ","second value ", "another value", "yet another value");

given the fact that fooValue has "bar1" on the above call, how can I make myProblem.text to display " first value another value"

this[ "foo" + fooValue] gives me undefined

  • edited the question and trying to be more specific.
2
  • Do you want to use either the foobar1 or the foobar2 value or them both inside the function? Can you provide more detail to what you want to do, because your question is very ambiguous with such small detail to the problem you want to solve. Commented Jul 25, 2012 at 10:01
  • @joncys The function wrote above is a simplified one. I'll try below to extend it maybe you'll get my point. I will use both values in that function STATICALLY of course. But also in a certain part of the function I want to dynamically tell which value would be used, because the call would be from an external application. Now let's rewrite the code: Commented Jul 26, 2012 at 13:11

1 Answer 1

1

Simple.

function test( fooValue:String, foobar1:String, foobar2:String, goobar1:String, goobar2:String ) {
//using the values statically
mytext1.text = foobar1;
mytext2.text = foobar2;
mytext3.text = goobar1;
mytext4.text = goobar2;

if ( goobar1 == "problem" ) {
    myProblem.text = this["foo" + fooValue] + this["goo" + fooValue];
}
}
//now here's an example call
test( "bar1","first value ","second value ", "another value", "yet another value")

Notice the change? Its the extra = in the if(goobar1... line.

goobar1 = "problem" sets the value of goobar1.

goobar1 == "problem" returns whether the value of goobar1 is "problem"

Rookie mistake, made sometimes by experienced hands too :)

Furthermore

this["foo" + fooValue] is equivalent to this.foobar1 (which is invalid since the this object does not have any property called foobar1)

You do it like this:

switch(fooValue) {
    case "bar1":
        myProblem.text = foobar1 + goobar1;
        break;
    case "bar2":
        myProblem.text = foobar2 + goobar2;
        break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually the typo with equal sign was only in this example...It's not the actual function I use. I thought I can somehow get rid of switch/case scenario but if there's no way around I will use this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.