?
Solved

Using a collection

Posted on 2002-07-15
4
Medium Priority
?
189 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:Atdhe Nuhiu
  • 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:Atdhe Nuhiu
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

585 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