Results 1 to 10 of 10

Thread: myCollection.Remove problem [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    378

    myCollection.Remove problem [Resolved]

    This won't work:
    VB Code:
    1. Public Sub ClearCollection(myCollection As Collection)
    2.  
    3.     For i = 1 To myCollection.Count
    4.    
    5.         myCollection.Remove (i)
    6.         Debug.Print i & " - " & myCollection(i).sFileName
    7.  
    8.     Next
    9.    
    10. End Sub

    I'm getting a "subscript out of range" error


    It is really strange. If i don't commment out the remove command and just print out the values, then it works fine. Does removing the items somehow mess up the index?

    I'm really just trying to clear all the values out of the collection.

    thanks
    Last edited by Kira Roark; Jun 4th, 2004 at 01:11 PM.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    VB Code:
    1. For i = myCollection.Count To 1 Step -1

    Reason: you are trying to remove an item of too big index: when you remove the first item, the second item becomes first, third the second and so on.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    378
    Of Course! Thanks....I feel like a moron....but at least I can move off this stupid problem.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    To prevent this mistake, you can use a code like this:

    VB Code:
    1. Do While myCollection.Count
    2.     myCollection.Remove 1
    3. Loop

    Simpler and you can't fail with it

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    378
    So, of course both methods work.

    Which is "sounder" or would be a better technique.

    thanks

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    There is no better way, and I'm not 100% sure about the performance. It might be faster to remove items starting from the end, or then not. I only suggested the code before because of the simplicity of it, it doesn't require declaring any variables with Dim and you can see what it does with one eye. But I don't think there is any real difference, use what you like to use

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    378
    I'll use the do loop. thanks again.

  8. #8
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose, Ca. - USA
    Posts
    302
    wouldn't this be the best/fastest/easiest way:

    VB Code:
    1. set MyCollection = New Collection

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    378
    Well, I'm declaring the collection as a Public in a module and then basically "resetting it" or reloading the data in it a various points in the prog....so I wouldn't think that your method would work.

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Actually it can work, but I've never done it like that. Most often I haven't had the need to erase a whole collection or I've added new items the within the removal loop and the code has run until the items run out. I'll do a little test on one thing that worries me with doing Set...

    Edit Yup, it seems to work ok memory wise as well (doesn't leave unused stuff in memory). So feel free to use it as well

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