collections...

I have a collection that has an entry and a key.
I can see if an entry exists by the Contains method.
How do I get the index of the element so that I can use the item method? Can the index be the key (A string)

How do you properly create this collection?

Isn't:
Dim myCol as Collection
good enough?

Must I:
Dim myCol as Collection
set myCol = new Collection
[413 byte] By [JustSomeGuy] at [2007-11-20 11:46:09]
# 1 Re: collections...
You can

dim myCol as Collection
Set myCol = New Collection

or simply Dim myCol as New Collection

Index of .Item() can be the key string

myCol.Add "Item 1", "Key1"

debug.print myCol.Item("Key1")

Seems to me you are talking about VB.NET, since our old VB 6.0 collection hasn't got .Contains
You are maybe in the wrong forum after all.
WoF at 2007-11-9 19:32:25 >
# 2 Re: collections...
That was going to be my next question...
I specifically searched the VB 6.0 Documentation and came up the with the contains method.. but it doesn't exits..
I guess I could use index and see if there is an error...
JustSomeGuy at 2007-11-9 19:33:24 >
# 3 Re: collections...
Yes like:

On Error Resume Next
a$ = mycol.Item("nosuchitem")
If Err Then Beep
On Error GoTo 0

.Contains is only supported by VB.NET collections. We VB6 veterans have to go odd ways, sometimes. ;)
WoF at 2007-11-9 19:34:28 >
# 4 Re: collections...
To those who might find this useful:

Private Function contains(col As Collection, key As String) As Boolean
Dim res As String
On Error Resume Next

If col.Count = 0 Then
contains = False
Else
res = col.Item(key)
If Err Then
On Error GoTo 0
contains = False
Else
contains = True
End If
End If
End Function

Thanks all.
JustSomeGuy at 2007-11-9 19:35:28 >
# 5 Re: collections...
Fine. Or a little shorter:

Function Contains(col As Collection, key As String) As Boolean
Dim res As String
On Error Resume Next
res = col.Item(key)
Contains = Err <> 0
End Function

Testing for 0 items is not necessary. Also On Error Goto 0 is not necessary since On Error Resume Next is only valid within the function.
WoF at 2007-11-9 19:36:30 >