BUG: Removing Collection Elements Takes Longer Than Expected (189735)



The information in this article applies to:

  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q189735

SYMPTOMS

In Visual Basic 6.0, removing elements from the end of a collection takes longer than removing elements from the beginning.

CAUSE

When removing an element from a collection, Visual Basic 6.0 begins at the beginning of the collection and traverses the collection until the desired element is reached, then that element is removed.

RESOLUTION

Remove elements from the beginning of the collection rather than the end.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Select Add Module from the Project menu. This will create Module1.
  3. Select Project1 Properties from the Project menu. Change the Startup Object to Sub Main.
  4. Copy and paste the following code into Module1:
          Sub Main()
    
             Dim c As New Collection
             Dim a As Long
    
             Set c = New Collection
    
             'Add elements to collection
             For a = 1 To 10000
                c.Add "Thank you sir, may I have another."
             Next a
             MsgBox "Done adding elements"
    
             'Remove elements from end, this takes long time
             For a = 10000 To 1 Step -1
                c.Remove a
             Next a
             MsgBox "Done removing elements from end"
    
             'Add elements to collection again
             For a = 1 To 10000
                c.Add "Thank you sir, may I have another."
             Next a
             MsgBox "Done adding elements"
    
             'Remove elements from beginning, this is much quicker
             For a = 1 To 10000
                c.Remove 1
             Next a
             MsgBox "Done removing elements from end"
           End Sub
    						
  5. Press the F5 key to run the project. Note the time difference between the two methods of deleting elements from a collection.

Modification Type:MajorLast Reviewed:5/13/2003
Keywords:kbbug KB189735