collection and index

Posted on 2005-04-05
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

    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 85

    Accepted Solution

    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

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    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…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now