Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


collection and index

Posted on 2005-04-05
Medium Priority
Last Modified: 2010-05-02

I want to know if there is a way to delete an object in a collection without losing the index?

For example,

I have
obj1 at index 0
obj2 at index 1
obj3 at index 2

Now, i want to remove obj2. However, if i use remove function in collection, obj2 will be removed, and it will become
obj1 at index 0
obj3 at index 1

This is not i want to have, i would like to have
obj1 at index 0
nothing at index 1
obj3 at index 2

Question by:TungVan
LVL 13

Assisted Solution

iHadi earned 100 total points
ID: 13710826
You should use the key of the item instead of the index

Dim MyCol As New Collection

' the key must be a string value
MyCol.Add "item 1", "1"
MyCol.Add "item 2", "2"
MyCol.Add "item 3", "3"
MyCol.Add "item 4", "4"
MyCol.Add "item 5", "5"
MyCol.Add "item 6", "6"
MyCol.Add "item 7", "7"

' if you retrieve an item with a numeric value then you are
' retrieving it using it's index
' but if you retrieve it using a string value then you are
' retrieving it using it's key
MyCol.Remove ("5")
label1.Caption = MyCol.Item("6")
LVL 86

Accepted Solution

Mike Tomlinson earned 150 total points
ID: 13712853
You can remove the item at the desire index and then put Nothing back in at the same spot.

Something like...

Private col As New Collection

Private Sub Form_Load()
    col.Add "Item1"
    col.Add "Item2"
    col.Add "Item3"
    removeIndex 2
End Sub

Private Sub removeIndex(ByVal index As Integer)
    If index >= 1 And index <= col.Count Then
        col.Remove index
        col.Add Nothing, , index
    End If
End Sub

Private Sub listContents()
    On Error GoTo emptySpot
    Dim i As Integer
    Debug.Print "col contents:"
    For i = 1 To col.Count
        Debug.Print "Index: " & i & " / " & col.Item(i)
    Next i
    Exit Sub
    Debug.Print "Index: " & i & " / Nothing"
    Resume Next
End Sub

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question