I often use a for/next loop to cycle through a collection to find a particular member of that collection based on some criteria. This works fine in every case except one: when I want to then erase the very member of the collection I just found. For example, if I wanted to give the heave-ho to every obect in a collection class that has a member veriable Color equal to Red:
The problem is once the collection class member is erased it makes to loop invalid because taking out the member reduces the total count of the collection in the middle of the loop. Eventually I get a "Index is out of range" error.Code:dim x as byte for x=0 to m_CollectionOfSomething.Count-1 if m_CollectionOfSomething.Color = Color.Red then m_CollectionOfSomething.Erase(x) end if next
This circumstance has bothered me every so often; I bet more experienced programmers have a different type of loop system that works better than this when you want to erase a collection member.
Hope I explained this well enough and thanks!





Reply With Quote