Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Alright guys firstly I'm working with XNA 4, secondly I'm using ArrayLists.

Now the problem is, I have no idea why these exceptions occur however they DO occur and apparently incorrectly.

Here's the code where it happens:

C#
if (bullets.Count - 1 >= i)
                        {
                            bullets.RemoveAt(i);
                            bulDirVal.RemoveAt(i);
                        }



As you can see I even added the if statement so it wouldn't execute those lines unless 'i' is indeed within the range of the ArrayList. When the exception is thrown, my 'i' value is always contained within the range, and I have no idea what is going on.

Is there anything I'm overlooking?
Posted
Updated 23-Apr-11 7:37am
v3
Comments
Ed Nutting 23-Apr-11 14:48pm    
Are you sure 'i' is within the range of both 'bullets' and 'bulDirVal'? Your if statement should look something like: if(i < bullets.Count && i < bulDirVal.Count) ... (FYI using '< X.Count' is better than 'X.Count - 1 >= ' as it is both clearer and computationally more efficient, but that is another topic :P)

1 solution

As Edman pointed out, you only check one collection, you also need to check the other one.

Another possibility is that you have multiple threads that may be accessing the same collection. If so, you need to add some thread-sync in there.
 
Share this answer
 
v2
Comments
Raztor0 23-Apr-11 16:57pm    
Both the ArrayLists are always the same size and get expanded/reduced at the same time as well, and oh yes multiple threads definately seems like it could be the problem here :o thanks a lot both of you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900