Clearing a Collection
Max,
If you really do need to keep the existing colection object intact (and
there can be good reasons - like if you've made yourself a custom
typed-collection class object with "extras" on it you don't want to loose).
To empty out a collection you basically need to do this:
for i = myCollection.count to 1 step -1 'MUST count DOWN from the top
'you CANNOT use a For Each loop here!
myCollection.remove i
next i
If your collection contains object instances, then you may need to enhance
that to properly destroy/close/terminate those objects (especially if you're
in .Net or using Office/ActiveX Automation classes):
for i = myCollection.count to 1 step -1 'MUST count DOWN from the top
'you CANNOT use a For Each loop here!
set objTemp = mycolelction.item(i)
myCollection.remove i
objtemp.Dispose() ' if the class implements the iDisposable interface or a
Dispose method like most .Net classes should
set objtemp = nothing
next i
The reason for this is that as you remove items from the colelction, the
rest of the items "settle downwards" towards item #1. So loops that count
upwards get about half-way up and error out, having only deleted about half
the collection items.
of course you could also do something like this:
do while mycollection.count 0
mycollection.remove 1
loop
....and you'd achieve the same thing (unless there are objects involved
needing more careful cleanup, like Office automation objects or .Net objects
again).
"Max" wrote:
I have created a collection using:
dim collMycollection as new collection
and populated it. Now I want to clear it and reuse it. What is the command
to do so? I've tried:
collMycollection.clear
But that doesn't seem to be supported. Any ideas?
--
Thanks!
Max
|