I’m a lil’ rusty with my AS, but here goes:
I’ve created two custom classes to contain data for a web forum controller flash tool:
[INDENT]class postClass
{
public var post_id:Number;
//plus some other variables like timestamp etc
}
[/INDENT]and
[INDENT]class userClass /
{
public var user_id:Number;
public var numPosts:Number;
var postArray:Array = new Array(); //contains all of user’s postClass objects
}
[/INDENT]Both of these classes are saved as postClass.as and userClass.as in the parent directory of the .fla code I’m writing.
I’m reading in data from an XML file that is generated by my website, of this format:
[INDENT]<conversation>
<post id=“1” user_id = “78” timestamp=“xxxxx”>
<text>“random text” </text>
</post>
</conversation>
[/INDENT]When I try to assign a new post to a user (by adding the post data to the postArray variable) I overwrite posts that I had already placed under other users. More specifically, if I’m posting the first post for User Y, I overwrite the first post for User X with the exact same data.
I thought this was a reference vs. value passing error, but I tried to eliminate this by not copying the “postClass” objects, but assigning the data directly into the user’s postArray array:
[INDENT]var userArray:Array = new Array();
function addNewUser(masterPostIndex,UserNumber)
{
userArray[UserNumber] = new userClass(); //create new userClass object
userArray[UserNumber].user_id = masterPostArray[masterPostIndex].attributes.user_id;
userArray[UserNumber].numPosts = 0;
numUsers++;
}
//this function adds the post at ‘postIndex’ to a user at ‘userIndex’
function addPostToExistingUser(postIndex,userIndex)
{
UserPostCount = userArray[userIndex].numPosts;//indexing of postArray
userArray[userIndex].postArray[UserPostCount] = new postClass(); //create new postClass object in postArray
userArray[userIndex].postArray[UserPostCount].owner_id = masterPostArray[postIndex].attributes.user_id;
//assign attribute values to postClass object in postArray
userArray[userIndex].postArray[UserPostCount].post_id = masterPostArray[postIndex].attributes.id;
userArray[userIndex].numPosts++;
displayPostData();
}
[/INDENT]This all seems to work fine until I try to write a new post for a second or third user. If I trace the contents of User X’s posts, they immediately become identical to the content of User Y’s posts, as soon as User Y’s posts are added. Later, when User Z comes along, both User X and User Y’s posts are overwritten. This code is kinda long, so I didn’t want to post the whole thing - but I can post any parts you’re interested in, of course.
This really ‘feels’ like a reference-passing error (arrays are all looking at the same location in memory), but I can’t figure out what I’m doing wrong - I’m not making a postClass object and then pushing it onto the user’s postArray (I tried that too, same result).
[INDENT]var newPost:postClass = new postClass();
//read and assign values to newPost
userArray[userIndex].push(newPost); //this is clearly passing the reference
[/INDENT]I can see how the latter example would pass the reference to newPost to the user object, then when I change newPost in the next iteration (when user Y posts something), User X’s data will change too. I tried to avoid this with the first set of code.
Regards,
AB