Solved

Removing item from collection

Posted on 2003-10-23
6
309 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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 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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

821 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