Results 1 to 6 of 6

Thread: Collection

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    Collection

    Hey,

    I used collection, to make object list.

    dim v as new collection

    I add items using key.

    v.add(object, key)

    Key is unique integer.

    After I add (or replace) item, I need to remove all other items, which key is bigger.

    How to do this?

    Maybe I should use other class (not collection)?

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359


    VB Code:
    1. ''declare the collection
    2.         ''
    3.         Dim myColl As New Collection()
    4.  
    5.         ''add items to it
    6.         ''
    7.         Dim i As Integer
    8.         For i = 0 To 99
    9.             myColl.Add(i, i)
    10.         Next
    11.  
    12.         ''now remove any items where key > intSomeNumber
    13.         ''
    14.         Dim intSomeNumber As Integer = 67
    15.         Dim myItem As New Object(), blnClear As Boolean = False
    16.         Do Until blnClear
    17.             blnClear = True
    18.             For Each myItem In myColl
    19.                 If myItem > intSomeNumber Then
    20.                     myColl.Remove("" & myItem)
    21.                     Debug.WriteLine("Remove : " & myItem)
    22.                     blnClear = False
    23.                 End If
    24.             Next
    25.         Loop
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    What is it about the blnClear boolean that makes it necessary in this loop?

    Does it have something to do with the size of the collection decreasing after each Remove method?

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by mendhak
    What is it about the blnClear boolean that makes it necessary in this loop?

    Does it have something to do with the size of the collection decreasing after each Remove method?
    Yeah it appears to cache the size of the collection at the start of the loop - so that after a few iterations it's already past the end of the collection...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Which is why that loop must run several times.

    Ok, Thanks.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309
    Maybe this code would run faster?

    VB Code:
    1. Dim vv As New ArrayList
    2.  
    3. Dim i As Integer
    4.        
    5.    For i = 0 To 99
    6.  
    7.          vv.Add(i, i)
    8.  
    9.    Next
    10.  
    11.  
    12. 'now remove any items where index > removeFrom
    13.        
    14. Dim removeFrom As Integer = 67
    15.  
    16. vv.RemoveRange(removeFrom, vv.Count-removeFrom)
    Last edited by Norkis; May 18th, 2004 at 04:50 AM.

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