# Changing the number of Iterations of a for loop using its own execution

• ### Question

• So, I'm designing a tower defense game. I need to remove "bullets" from a list when they hit an enemy. Here, k is each bullet, and j is each tower. Each tower has a list of projectiles. I need to (at least I think I need to) remove the projectile from the list in this loop. However, when I remove it, the loop still executes the number of times it would have if there were no change so I get an error when I reference k inside the loop to check if the projectile hit the troll. I would appreciate help with this method as I don't have much time to learn a new strategy to accomplish my goal. You can ignore the 4 Pythagorean theorems to check if a bullet hit an enemy.

TL;DR: How do you/is it possible to modify the maximum value of the counter of a for loop inside the loop.

`                    For k As Integer = 0 To tower1Projectiles(j).Count - 1                        If ((Math.Sqrt(tower1ProjectilesAimLocation(i)(k)(0) - trollAimLocations(j)(0)) ^ 2) < tower1Projectiles(i)(k).Size.Width + trolls(j).Size.Width) Or ((Math.Sqrt(tower1ProjectilesAimLocation(i)(k)(0) - trollAimLocations(j)(0)) ^ 2) * -1 > tower1Projectiles(i)(k).Size.Width + trolls(j).Size.Width) * -1 Or ((Math.Sqrt(tower1ProjectilesAimLocation(i)(k)(1) - trollAimLocations(j)(1)) ^ 2) < tower1Projectiles(i)(k).Size.Height + trolls(j).Size.Height) Or ((Math.Sqrt(tower1ProjectilesAimLocation(i)(k)(1) - trollAimLocations(j)(1)) ^ 2) * -1 < tower1Projectiles(i)(k).Size.Height + trolls(j).Size.Height) * -1 Then                            trollsHealth(j) -= tower1ProjectileDamage                            Controls.Remove(tower1Projectiles(i)(j))                            tower1ProjectilesTimerPerTower(i).RemoveAt(j)                            tower1Projectiles(i).RemoveAt(j)                            xMovement(i).RemoveAt(j)                            yMovement(i).RemoveAt(j)                        End If`

Saturday, January 5, 2019 5:15 AM

• Hi

With a guess due to post unreadable in part and lots of variables unknown.

If you try to Remove iterms from a collection inside a loop in that way, then an exception will occur due to the loop becoming unstable as items are no longer there.

If that does happen to be the issue you are having, then the solution is to count down in the loop instead of up. e.g.

`For k As Integer = tower1Projectiles(j).Count - 1 To 0 Step -1`

Regards Les, Livingston, Scotland

• Marked as answer by Sunday, January 6, 2019 9:40 PM
Saturday, January 5, 2019 5:25 AM

### All replies

• Hi

With a guess due to post unreadable in part and lots of variables unknown.

If you try to Remove iterms from a collection inside a loop in that way, then an exception will occur due to the loop becoming unstable as items are no longer there.

If that does happen to be the issue you are having, then the solution is to count down in the loop instead of up. e.g.

`For k As Integer = tower1Projectiles(j).Count - 1 To 0 Step -1`

Regards Les, Livingston, Scotland

• Marked as answer by Sunday, January 6, 2019 9:40 PM
Saturday, January 5, 2019 5:25 AM
• Thanks, I will try that. Wow. I have no idea what happened to the post.

I'm very impressed with your perfect interpretation of my problem; now that I read it, it was very unclear and difficult for someone unfamiliar with my project to understand.

Edit, oh it was Grammarly HTML or something. Fixed

Saturday, January 5, 2019 5:28 AM