Solved

Using a collection

Posted on 2002-07-15
4
181 Views
Last Modified: 2010-05-02
What I want to do is iterate through a collection and if an object meets a certain critreion, delete that object from the collection.

for each myobject in mycollection

  if myobject.mytest = "deleteme" then mycollection.remove

next

It is the mycollection.remove that needs the element index and I don't know how to determine the current index.

TIA
0
Comment
Question by:Philip Pinnell
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 7154031
for each myobject in mycollection

 if myobject.mytest = "deleteme" then mycollection.remove mycollection.Index

next
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 7154041
Sorry that should have been:

for each myobject in mycollection

if myobject.mytest = "deleteme" then mycollection.remove myobject.Index

next
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 7154051
Cor that was easy. I perhaps should have got it myself.

Thanks
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7154067
First the object you are placing in the collection must have a unique name to be used as an ID.
You need to use the class builder wizard to add a collection class so that you can use the For Each syntax.
(Here are the steps: Projects-Add Class Module-Select VB Class builder-File-New Collection-Based on New Collection-BAsed on a new Class called Dummy. Now remove the dummy class.) Change the add method as follows:

Public Sub Add(YourObject As YourObjectName, Optional sKey As String)
   
    If Len(sKey) = 0 Then
        mCol.Add YourObject
    Else
        mCol.Add YourObject, sKey
    End If

End Sub


To Add New Items:

Dim YO as YourObject
Dim CS as Collection1

' create you collection
Set CS = New Collection1

' Creat a new object
Set YO= New YourObject
YO.ID = "Item1"
YO.etc.....
CS.ADD Yo, "Item1"

Set YO = Nothing ' this will kill YO but keep the instance in the collection

' now you can loop elements

For Each YO in CS
  If Yo.Obsolete Then ' whatever property indicates removal required
    CS.Delete YO.ID ' Pass a string value to indicate key to delete
  End If
Next











0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

772 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