Solved

Using a collection

Posted on 2002-07-15
4
184 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
[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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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, 18 hours left to enroll

624 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