Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XNA
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:
 
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 23-Apr-11 8:30am
Raztor0334
Edited 23-Apr-11 8:37am
v3
Comments
EdMan196 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
Raztor0 at 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)

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 23 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100