Results 1 to 4 of 4

Thread: Simple code for remove from Collection .

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    45

    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

  2. #2
    Member bizzyVB's Avatar
    Join Date
    Jun 2001
    Location
    Florida
    Posts
    38
    if that collection control is a listbox then here's the code..

    VB Code:
    1. dim i as integer
    2.  
    3. For i = 0 To collection.Count - 1
    4. collection.Remove (i)
    5. Next i

    im not quite sure if it'll work though try it

  3. #3
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    No, a collection is like an Array, not a Listbox
    Don't Rate my posts.

  4. #4
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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:
    1. For i = col.Count to 1 Step - 1
    2.     col.Remove i
    3. 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:
    1. Public Sub ClearCollection(ByRef col As Collection)
    2. dim colTMP As New Collection
    3.  
    4. ' this should clear the contents of the collection
    5. Set col = colTmp
    6.  
    7. 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
  •  



Click Here to Expand Forum to Full Width