Skip navigation
HMU Desgn
Currently Being Moderated

linked(?) variables changing others

Aug 18, 2012 5:38 PM

Tags: #as3 #variables

I'm not even sure what to call this. I tried to create a contrived example, but it won't work =/ (or rather it does)

 

So here's the setup. I have my document class, which has a member variable (call it A) which is a custom class. The varable has a public property (call it B) which is an array of arrays (contains 3). Inside of the document class, I have a function which assigns one of the arrays to a variable (call it C), then uses it and later assigned B to an array of nulls. The problem is that when I do this assignment, it also modifies the array inside of A.

 

example (but as I mentioned, I couldnt create a broken contrived example)

 

A.B = [[1,2,3],[4,5,6],[7,8,9]];

 

C = A.B[1];

trace(A.B, C); // 1,2,3,4,5,6,7,8,9 4,5,6

C = [null,null,null];

trace(A.B, C); // 1,2,3,null,null,null,7,8,9 null,null,null

 

No matter what I try, C seems to be "linked" to the array inside A.B. I have tried it with both the shortcut and full syntax of Array(); The only solution I could  get to work was to create a getB  function on A which accepts the index, loop over the contents of A.B[i] assigning the values to a new variable D which is then returned. This seems overkill.

 

I have been noticing similar behavior, so any general assistance would be very helpfull as well. Even knowing what this is called so I can google for it effectively would be helpful!

 
Replies
  • Currently Being Moderated
    Aug 18, 2012 6:22 PM   in reply to HMU Desgn

    Looping is one way to go.  Another approach would be to use any of the Array methods that return an array.  The "slice" method is one...

     

    C = A.B[1].slice();

     

    When you assign it directly, you are assigning a reference to that array, just like any other object you assign to another var/name.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points