Clearing collections (1 Viewer)

CHAOSinACT

Registered User.
Local time
Today, 10:25
Joined
Mar 18, 2009
Messages
235
I think this should be easy; for some reason it isn't.

I'm trying to clear an existing class collection. To start with :

Code:
    lngIndex = 1
For lngIndex = 1 To colBars.Count
   colBars.Remove (lngIndex)
Next lngIndex

has interesting results. watching it under action, what happens is it works out the correct count of the collection and then get HALFWAY through clearing and stops. if I step through it it keeps re-running the code till clear, every time saying it cleared half before and now half left, so it start with 16, removes 8. starts at 8, removes 4. etc. till 1 left then gone. it works on step through, locks up otherwise.

for some reason, i can't use

set colBars = nothing

i can for the initialization in the beginning but NOT for erasing existing collections?!? seems odd, but Access "will not let that pass".

if ChrisO is watching, yes it's your collection I'm modifying ;)

Am I missing something? I would expect

set colBars = nothing

to just work...
:banghead:


edit: its a subscript out of range error too. though I don't see how thats the case...
 
Last edited:

CHAOSinACT

Registered User.
Local time
Today, 10:25
Joined
Mar 18, 2009
Messages
235
Solution for those that need it ( I couldn't find this online so here it is):


When you remove from a collection of say 16 1 item the collection count changes to 15 immediately. so on the next round of the loop we remove item 2 of 15, 3 of 14 and so on, removing only half of any given collection. the correct code is:

Code:
For lngIndex = 1 To colBars.Count
   colBars.Remove (1)
Next lngIndex

Where you step through the INITIAL collection count and always remove item 1 * colbars.count.

*phew*:eek:
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:25
Joined
Aug 30, 2003
Messages
36,139
Try stepping through backwards.
 

spikepl

Eledittingent Beliped
Local time
Today, 02:25
Joined
Nov 3, 2010
Messages
6,142
Inside a loop, sane people never mess with neither the loop counter nor the limit. :D

When you delete like you did, you did both. When you delete an item, the current counter now points at the next item. But then you reach the Next, and so advance the counter, hence skipping the current item.

It's like digging a hole under yourself. But, as Paul wrote, doing it in reverse order is normally OK.
 

CHAOSinACT

Registered User.
Local time
Today, 10:25
Joined
Mar 18, 2009
Messages
235
but is there anything wrong with the solution I posted after? it seems to work fine.
 

spikepl

Eledittingent Beliped
Local time
Today, 02:25
Joined
Nov 3, 2010
Messages
6,142
Your solution is one way of doing it.

Unfortunately it is not immediately obvious why it is written the way it is written - the code appears wrong since it seemingly repeats the same statement .Count times. WHen you come back to your code 6-12 month later (and code is just like the in-laws, it comes back whether you like it or not), you will not be able to see what is happening here and might be tempted to fix the "obvious " error.

All "smart" solutions sooner or later bite the derrière of the developer. KISS rules (keep it simple, stupid!) :D
 

CHAOSinACT

Registered User.
Local time
Today, 10:25
Joined
Mar 18, 2009
Messages
235
fair enough! I'll make sure at the very least I comment the hell out of it then.
 

Users who are viewing this thread

Top Bottom