checking if an item exists in a custom collection

R

rocco

Hello,
I have a custom collection and I will add few class objects to it.
How can I check if an object exists in the collection?

Thanks,
rocco
 
V

vanderghast

You can use a Dictionary rather than a Collection.

To use a Dictionary, you have to include a reference to Microsoft Scripting
Runtime ( Tools | References ... )

In VBA, you can refer it with the prefix Scripting, if there is a collision
of names.


Dim aaa As Scripting.Dictionary ' just for illustration of possible
disambiguation
Set aaa = New Dictionary

aaa.Add "hello", Nothing
If Not aaa.Exists("hello") Then aaa.Add "hello", 222.333
If Not aaa.Exists("world") Then aaa.Add "world", Now


Note that a Dictionary contains a list of keys and a list of items. The keys
do not allow duplicated value ( a trappable error will occur if a the key
already exist and you try to add another item with the same key), while it
allows to find an item, given a key, faster than by doing a for each loop
walk trough search.


Vanderghast, Access MVP
 
D

Dirk Goldgar

rocco said:
Hello,
I have a custom collection and I will add few class objects to it.
How can I check if an object exists in the collection?


As an alternative to using a Dictionary object, you can try to retrieve an
item from the collection by its key value, and trap the error that occurs if
the item is not found.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top