Checking for a named member of a collection?

  • Thread starter Thread starter Jim S.
  • Start date Start date
J

Jim S.

Greetings,

Is there an elegent way to check if a specific, named
object is a (current) member of a collection? I'd like to
be able to say, for example:

If myCollection.IsMember("someKey") Then
...
Else
...
End If

I know I can try to access the member, trapping the "The
item with the specified name wasn't found" error and
branching off of that, such as:

Err.Clear
On Error Resume Next
Set myObject = myCollection("someKey")
If Err.Number = &H80070057 Then
...
ElseIf Err.Number <> 0 Then
MsgBox ("Error " & Err.Number & _
": " & Err.Description)
Exit Sub
End If
On Error GoTo 0

but that seems somewhat ugly. I suppose I could also do:

found = False
For Each memberObject In myCollection
If memberObject.Name = "someKey" Then
found = True
Exit For
End If
Next

but that seems ugly too. Am I missing something obvious?

Thanks,
Jim S.
 
What I usually do is:

Set myObject = Nothing
On Error Resume Next
Set myObject = myCollection("someKey")
On Error Goto 0
If myObject Is Nothing Then
'Handle missing member
End If
' code continues...
 
Well, you are on the right track. Why not embed the code in a function?
Something like:

Function getFromCollection(whatColl As Collection, _
whatKey As Variant, ByRef Rslt)
Err.Clear
On Error Resume Next
Set Rslt = whatColl(whatKey)
Select Case Err.Number
Case 0:
Case 13: Rslt = whatColl(whatKey)
Case 5, 9: Exit Function
'not found in collection; return False(default)
Case Else:
MsgBox ("Error " & Err.Number & _
": " & Err.Description)
Exit Function '<<<<
End Select
On Error GoTo 0
getFromCollection = True
End Function

Now, you can use this function as in:

Sub testGetFromColl()
Dim aColl As Collection, y
Set aColl = New Collection
aColl.Add 1, "1"
aColl.Add 2, "2"
If getFromCollection(aColl, "1", y) Then MsgBox y
If getFromCollection(aColl, "3", y) Then MsgBox y
If getFromCollection(aColl, 3, y) Then MsgBox y

End Sub



--
Regards,

Tushar Mehta
www.tushar-mehta.com
Business solutions leveraging technology
Microsoft Most Valuable Professional (MVP) 2000-2004
 
Just looking at this again, it seems you are using a OOP class
approach. In which case, checking a collection class for the existence
of a member is usually coding which left to the client to do. It would
be nice for all classes to provide an IsMember property but I'd
consider this a bit redundant because the client can use the
collection class's Item method. Collections are fairly straightforward
i.e. the member either is or is not present, not much else to say, so
I would handle the errors as follows:

<client code>
Dim myObject As MyClass
Dim myCollectionObject As MyCollectionClass
' Initialize myCollectionObject here
Set myObject = Nothing
On Error Resume Next
Set myObject = myCollectionObject("someKey")
On Error Goto 0
If myObject Is Nothing Then
'Handle missing member
End If
' code continues...

</client code>

<in collection class>
Public Property Get Item(ByVal Index As Variant) As MyClass
Attribute Item.VB_UserMemId = 0
Dim blnError As Boolean
On Error Resume Next
Set Item = m_colColumns.Item(Index)
blnError = Not CBool(Err.Number = 0)
On Error Goto 0
If blnError Then
Err.Raise vbObjectError + 9, "MyCollectionClass.Item", _
"Requested member not found in collection."
End if
End Property
</in collection class>

--
 
Back
Top