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

x
?
Solved

Using a collection

Posted on 2002-07-15
4
Medium Priority
?
186 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

972 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