views:

25

answers:

1

Hello.... I'm having a very weird problem with a vector in my application.

Details...

I have the following classes.

Person,Player,PlayerController.

Player extends Person. Person extends ObjectProxy in order to enable binding. So the Player class has the [Bindable] tag.

The PlayerController class contains a remote object calling a php method to receive a firstname and a lastname and when the CallResponder gets the result from the call,the result handler creates a Player instance. At that moment I am trying to push the player object into a Vector..

The problem is the following.

Every time the push method is called, the vector is being populated with the last player that was created but not just in the end of the vector. It replaces the other instances as well! So the vector always contains the most recent player instance but in every position of it. :S

I have also tried doing it with an Array and the results are the same.

Any thoughts on what I'm doing wrong? It's driving me crazy. :S

A: 

My guess is that you are pushing the same object reference into your vector after setting that reference to a new instance of Player, meaning that all of the items in your vector refer to the same object, which is always the newest object. I say "guess" because I haven't seen your code. What are you pushing into your vector, a local variable? A member variable?

Edit: Based on your comment below, try adding your new Player object to your vector using a local variable rather than from your member variable (player_):

var newPlayer:Player = new Player();
newPlayer.firstName = results[firstName];
newPlayer.lastName = results[lastName];
players_.push(newPlayer);
player_ = newPlayer;

You are doing what I suspected, which is adding multiple references to the same object to your vector. Since all of the references in your object refer to the same object, changing the one object changes ALL of the entries in your vector. Doing the above will create a brand new (and unique) Player object each time you add to your vector.

Wade Mueller
the player instance is a private variable in the PlayerController class.I have tried pushing it to a private vector inside the class and get the weird result.also I have tried pushing it in a vector on the component that includes the controller and the same is happening.so the code in the result handler looks something like thisplayer_.firstName = results[firstName];player_.lastName = result[lastName];players_.push(player_);Am I doing something I shouldnt? or am I missing something? :S
NickDF
See my edits above. This should fix your problem.
Wade Mueller
I cant thank you enough! You saved me from a looooot of trouble because I was searching for the answer during the last 3-4 days and I couldnt figure it out.I thought about creating a new object in the function just when you were editing your answer. :D
NickDF
Great, glad I could help! Feel free to upvote my answer so it applies to my reputation, thanks. ;)
Wade Mueller