|
-
Jun 11th, 2002, 11:34 PM
#1
Thread Starter
Member
Simple code for remove from Collection .
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
-
Jun 11th, 2002, 11:38 PM
#2
Member
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
-
Jun 12th, 2002, 12:29 AM
#3
PowerPoster
No, a collection is like an Array, not a Listbox
-
Jun 12th, 2002, 12:35 AM
#4
PowerPoster
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.
VB Code:
For i = col.Count to 1 Step - 1
col.Remove i
Next i
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:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|