Solved

Removing item from collection

Posted on 2003-10-23
6
305 Views
Last Modified: 2010-05-03
I have a collection of arrays. The items are being added correctly, but when I try to remove them, i get an out of bounds error:

Here is the code:

**NOTE**
'ID is a unique value that gets set somewhere else

arrCabinetState = Array(0)
   ReDim arrCabinetState(7)
   
   'store the current cabinet state for this Transaction Request
   arrCabinetState(1) = CommPort
   arrCabinetState(2) = PollCode
   arrCabinetState(3) = Cabinet
   arrCabinetState(4) = CellNum
   arrCabinetState(5) = Quantity
   arrCabinetState(6) = Status
   arrCabinetState(7) = ID

 
   
   IDCount = IDCount + 1
   If IDCount = 1 Then
      ReDim arrGlobalCabinetStat(IDCount)
   Else
      ReDim Preserve arrGlobalCabinetStat(IDCount)
   End If

   ReDim Preserve arrGlobalCabinetStat(IDCount)

   arrGlobalCabinetStat(IDCount) = arrCabinetState
   
   'add this state to the cabinetstate collection
   colCabinetState.Add Item:=arrGlobalCabinetStat(IDCount), key:=CStr(ID)

'************************************************
'This function works
'************************************************
Private Function IDExists(intCollectionCount As Integer, CabinetKey As String) As Boolean

    Dim localCabinetState As Variant
    Dim lngIndex As Long
    Dim strResult As Variant
   
    lngIndex = 1
    IDExists = True
   
    On Error GoTo noSuchItem
   
    While Not (lngIndex >= (intCollectionCount - 1))
       localCabinetState = colCabinetState.Item(CStr(CabinetKey))
       lngIndex = lngIndex + 1
    Wend

    Exit Function
   
noSuchItem:
    IDExists = False ' indicates cabinet key not found
End Function

'*************************************
'This function does not work all the time
'************************************
Private Function cmdRemoveID(intCollectionCount As Integer, CabinetKey As String) As Boolean

    Dim localCabinetState As Variant
    Dim lngIndex As Long
    Dim lngResult As Long
   
    lngIndex = 1
    cmdRemoveID = True
   
    On Error GoTo noSuchItem
   
    While Not (lngIndex >= (intCollectionCount + 1))
       localCabinetState = colCabinetState.Item(CStr(CabinetKey))
       lngResult = colCabinetState.Item(CInt(lngIndex))(7)
       If lngResult = CLng(CabinetKey) Then
          colCabinetState.Remove (lngIndex)
          cmdRemoveID = True
          lngIndex = intCollectionCount + 1
       End If
       lngIndex = lngIndex + 1
    Wend

    Exit Function
   
noSuchItem:
    cmdRemoveID = False ' indicates cabinet key not found
End Function
0
Comment
Question by:LeeHenry
6 Comments
 
LVL 44

Assisted Solution

by:bruintje
bruintje earned 25 total points
ID: 9611141
you could opt to use the lbound and ubound on the collection instead of counting manually like

for lngIndex = lbound(colCabinetState) to ubound(colCabinetState)
  ....your code
next lngindex

hope this helps a bit
0
 
LVL 1

Assisted Solution

by:paulott
paulott earned 75 total points
ID: 9611197
The problem is, when you remove one from the collection, the rest shift down. At that point, you cannot .Remove(intCollectionCount) but can .Remove(1) again, and again, etc.

You simply need to remove 1 over and over to completely iterate through a collection.

For... Each... Next statements work well.
0
 
LVL 1

Assisted Solution

by:paulott
paulott earned 75 total points
ID: 9611270
Here's a fix:
______________________________________________________
    While (lngIndex < colCabinetState.Count)
       localCabinetState = colCabinetState.Item(CStr(CabinetKey))
       lngResult = colCabinetState.Item(CInt(lngIndex))(7)
       If lngResult = CLng(CabinetKey) Then
          colCabinetState.Remove (lngIndex)
          cmdRemoveID = True
       Else
          lngIndex = lngIndex + 1
       End If
    Wend
______________________________________________________

I changed your while statement to use the collection's Count so it will keep up with any removals and not allow you to go past the old count.  I also only increment the index if you don't remove something.  This way, if you do remove something you will check the next item in the collection the next pass through as the next item's index will shift and become the index of the one just removed.

Example:  a collection with  "A", "B", "C", "D", "E";  removing "C" gives you "A", "B", "D", "E" so you need to check spot 3 again.

Another way to do it:
________________________________________________

    Dim tmpItem
    For Each tmpItem In colCabinetState
       localCabinetState = tmpItem(CStr(CabinetKey))
       lngResult = tmpItem(CInt(lngIndex))(7)
       If lngResult = CLng(CabinetKey) Then
          colCabinetState.Remove (lngIndex)
          cmdRemoveID = True
       Else
          lngIndex = lngIndex + 1
       End If
    Next tmpItem
________________________________________________
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 9611709
There can be only one entry in a collection with a particular key.  In a collection, you don't need to iterate through the items like you would in an array if you already have the key.   It just jumps straight there.  That's the whole point of a collection!

With that in mind, your IDExists function only needs a key passed in and does way too much work.  It should look like this:

Private Function IDExists(CabinetKey As String) As Boolean
    On Error GoTo noSuchItem

    Dim localCabinetState As Variant
    localCabinetState = colCabinetState.Item(CStr(CabinetKey))
    Set localCabinetState = Nothing
    IDExists = True
    Exit Function
   
noSuchItem:
    IDExists = False
End Function

The same is true for your cmdRemoveID function:

Private Function cmdRemoveID(CabinetKey As String) As Boolean
    On Error GoTo noSuchItem
   
    colCabinetState.Remove (CabinetKey)
    cmdRemoveID = True ' Key was found and removed
    Exit Function
   
noSuchItem:
    cmdRemoveID = False ' indicates cabinet key not found
End Function

That's it!  Hope this helps you.  By the way....there is no need to "shift down" the items in a collection after you remove an item.  That is all done for you.
0
 
LVL 6

Author Comment

by:LeeHenry
ID: 9615223
Thanks!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9615363
Just out of curiosity...why are you using BOTH a global array and a collection for your cabinet state arrays?

arrGlobalCabinetStat(IDCount) = arrCabinetState
colCabinetState.Add Item:=arrGlobalCabinetStat(IDCount), key:=CStr(ID)

If you use only the collection then you can get rid of this whole mess:

IDCount = IDCount + 1
If IDCount = 1 Then
   ReDim arrGlobalCabinetStat(IDCount)
Else
  ReDim Preserve arrGlobalCabinetStat(IDCount)
End If

A collection automatically resizes itself.  You can still access a collection like an array using the .item function.

If you pass .item a number, it gives you the item at that position (index)
If you pass .item a string, it gives you the item corresponding to that key (if it exists)

One difference with a collection though is it is 1 based, not zero based.  That is, the first item is retrieved with a.item(1), not a.item(0).
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

12 Experts available now in Live!

Get 1:1 Help Now