Solved

Removing item from collection

Posted on 2003-10-23
6
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 86

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 86

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

623 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