A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: String to variable name - in a function?

  1. #1
    Senior Member
    Join Date
    Jul 2004
    Posts
    264

    String to variable name - in a function?

    Hi guys,

    I've read (and tried) that this works -

    Code:
    var test1:string = "hello"
    trace(this["test" + 1])
    BUT - when i try this in a function - with a local variable, it wont work

    Code:
    function myFunction():void{
    var test1:string = "hello"
    trace(this["test" + 1])
    }
    I think it's because the local variable cannot be found by "this"


    Can someone please help how to get the variable name in the function - using a string!

    Thankyou!

  2. #2
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    I'ts usually easier to use arrays instead of variables created dynamically.

    I'm not sure why your code dosn't work. However, I got this code to work:
    PHP Code:
    function myFunction():void
    {
        var 
    test1:String = new String
        this
    .test1 "hello";
        
    trace(this["test" "1"]);
    }
    myFunction (); 
    also see:
    http://www.kirupa.com/forum/showthre...e-NAMES-in-AS3

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    The local variable is within the function and not on the main timeline. So just eliminate "this".
    function myFunction():void
    {
    var test2:String = "hello";
    trace(["test" + 2]);
    }
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Senior Member
    Join Date
    Jul 2004
    Posts
    264
    Thanks guys,

    cancerinform -- that worked perfect. cheers!


    Can i just update this question -- is there anyway to store the paths in strings too?

    For example:

    Code:
    var myEG:String = 'movieclipName.textboxName';
    
    // then do something like the following:
    
    this[myEG].text = "hello!";
    Thanks again for the help

  5. #5
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    This is not so good, because 'Object' is not a specific type cast. It is better to replace it with 'TextField'.

    var myEG:Object = mc.myText;
    myEG.text = "hello!";
    - The right of the People to create Flash movies shall not be infringed. -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center