Link to home
Start Free TrialLog in
Avatar of LeeHenry
LeeHenry

asked on

Removing item from collection

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
SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LeeHenry
LeeHenry

ASKER

Thanks!
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).