Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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
0
LeeHenry
Asked:
LeeHenry
4 Solutions
 
bruintjeCommented:
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
 
paulottCommented:
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
 
paulottCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
LeeHenryAuthor Commented:
Thanks!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now