Hi,
Let say my collection got
-a
-b
-c
can anyone provide me the code to remove a and then b and lastly c from the collection.
i=0
do until i =2
collection.remove
i=i+1
loop
is this the correct code ??
=)
thanks
Printable View
Hi,
Let say my collection got
-a
-b
-c
can anyone provide me the code to remove a and then b and lastly c from the collection.
i=0
do until i =2
collection.remove
i=i+1
loop
is this the correct code ??
=)
thanks
if that collection control is a listbox then here's the code..
VB Code:
dim i as integer For i = 0 To collection.Count - 1 collection.Remove (i) Next i
im not quite sure if it'll work though try it
No, a collection is like an Array, not a Listbox :D
one of the arguments for removing something from a collection is the index position of the item in the collection.
But if you don't know the index, then looping through is the only other way, unless you happen to know the key of the object in the collection.
Here's another thing that doesn't address your problem specifically, but a lot of people don't understand. If you want to clear out a collection, you have to loop backwards or you'll hit a point where the index no longer exists and get an error.
I think an even faster way to do it is to just create a new collection and set the old collection equal to it. I'll have to try this and see if it works.VB Code:
For i = col.Count to 1 Step - 1 col.Remove i Next i
VB Code:
Public Sub ClearCollection(ByRef col As Collection) dim colTMP As New Collection ' this should clear the contents of the collection Set col = colTmp End Sub