0
votes

"This" is incapable of finding the appropriate variable "array1" despite it clearly being declared within the function. But if I declare the variables outside of the function it works. How can I have the variables inside the function but keep it working?

package 
{

public class main extends MovieClip
{
//If I declared the variables here it would work.
    public function main():void
    {
        var array1:Array = [1,2];
        var array2:Array = [3,4];
        trace(this["array"+1][1]); //returns reference error 1069   
    }

}
}

Am I stuck with declaring the variables outside of the function?

And no, multidimensional arrays won't work for what I need it for. Though it looks like it would solve everything within the code snippet provided huh?

My intentions is to pass arrays through a class to be used and change which array bunch I use. If I used multidimensional arrays, it would be inefficient due to the amount of copying that would occur.

3

3 Answers

2
votes

For this[] to access properties, those properties must belong to this. In your sample, the properties belong to the function in which they were defined and are inaccessible outside of that scope.

So firstly; yes, for your code to work you will of course need to define properties in the class level scope.

But more importantly I'd look closely at what you're trying to do and determine whether it's a good approach - my bet is that it's not. It seems like you may want to consider an isolated class that deals with all the data you want to store.

2
votes

Your error is because you mis-scoped the variables.
The "this" keyword means you are trying to target a variable on the specific instance of the class.
You have scoped the variables locally to the function.
You need to move them to the class declaration area for "this" to work.

package 
  {

  public class main extends MovieClip
  {
         public var array1:Array = [1,2];
         public var array2:Array = [3,4];
      public function main():void
      {
          trace(this["array"+1][1]); //returns reference error 1069   
      }

  }
}




// now if you meant to scope them locally to the function then you can not use "this"
// you have to assign them to an object or an array 

package 
  {

  public class main extends MovieClip
  {
      public function main():void
      {
          var obj:Object = {}
              obj['array1'] = new Array( [1,2] )
              obj['array2'] = new Array( [3,4] )
          trace(obj["array"+1][1]);
      }
  }
}

This is untested code but it should put you on the right track.

0
votes

Depending on what you are trying to accomplish exactly, you can also consider passing an array as an argument to a function, something like:

function main():void
{
    var array1:Array = [1,2];
    var array2:Array = [3,4];
    doSomethingWithArray(array2);
}
main();

function doSomethingWithArray(arr:Array):void
{
    trace(arr[1]); //Traces the value of array2[1], which = 4
}

Also consider having a var currentArray:Array that you can set as any array you'd like and refer to it as needed, if applicable.

public class main extends MovieClip
{

    ...
    public var currentArray:Array;

    function main():void
    {
        var array1:Array = [1,2];
        currentArray = array1;
        doSomethingithCurrentArray();

        var array2:Array = [3,4];
        currentArray = array2;
        doSomethingithCurrentArray();

        currentArray = null;
    }

    public function doSomethingithCurrentArray():void {
       if(currentArray != null){
          trace(currentArray);
       }
    }

}