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(IDCou nt)
Else
ReDim Preserve arrGlobalCabinetStat(IDCou nt)
End If
ReDim Preserve arrGlobalCabinetStat(IDCou nt)
arrGlobalCabinetStat(IDCou nt) = arrCabinetState
'add this state to the cabinetstate collection
colCabinetState.Add Item:=arrGlobalCabinetStat (IDCount), key:=CStr(ID)
'************************* ********** ********** ***
'This function works
'************************* ********** ********** ***
Private Function IDExists(intCollectionCoun t 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(intCollectionC ount 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
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(IDCou
Else
ReDim Preserve arrGlobalCabinetStat(IDCou
End If
ReDim Preserve arrGlobalCabinetStat(IDCou
arrGlobalCabinetStat(IDCou
'add this state to the cabinetstate collection
colCabinetState.Add Item:=arrGlobalCabinetStat
'*************************
'This function works
'*************************
Private Function IDExists(intCollectionCoun
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(
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(intCollectionC
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(
lngResult = colCabinetState.Item(CInt(
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just out of curiosity...why are you using BOTH a global array and a collection for your cabinet state arrays?
arrGlobalCabinetStat(IDCou nt) = 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(IDCou nt)
Else
ReDim Preserve arrGlobalCabinetStat(IDCou nt)
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).
arrGlobalCabinetStat(IDCou
colCabinetState.Add Item:=arrGlobalCabinetStat
If you use only the collection then you can get rid of this whole mess:
IDCount = IDCount + 1
If IDCount = 1 Then
ReDim arrGlobalCabinetStat(IDCou
Else
ReDim Preserve arrGlobalCabinetStat(IDCou
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).
ASKER