I sometimes have problems with using for…next when elements in the array are deleted as part of the loop. The usual way of avoiding is to count down rather than up i.e. for i as integer = a.ubound down to 0, but this too fails sometimes with an Out of Bounds.
The simple question is - does For Each avoid this problem?
If I have an array I know I will remove index(s) from I will use DownTo instead of to. That way you can delete the index you are on and you will continue down to 0. Otherwise you can just handle overcoming the index difference in the for loop after the remove.
Yes, that’s what I’ve been used to - but I’m still getting OOB errors. Not sure why.
BTW - in testing For Each - this is worse - when it can’t find an element of the array it seems to stop processing the rest of the array - but it doesn’t seem to tell you that its stopped. I think an OOB error is better.
Use a try…catch around each remove?
Can you add a break point before the loop starts to step through every line In the loop? Perhaps you can see something shifting that you werent aware of?
If you’re working backwards by index, you won’t get an OutOfBoundsException, so I think you’re dealing with a bug in your code. Something is deleting elements beyond what you expect.
You can’t use For Each at all for this purpose.
Thanks all. Unfortunately I’ve not been able to simulate it on my machine - just on users (always embarrassing) but I’ll put some more error catching around the problem.