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>
--
(E-Mail Removed) (onedaywhen) wrote in message news:<(E-Mail Removed)>...
> 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...
>
> --
>
> "Jim S." <(E-Mail Removed)> wrote in message news:<1a57301c42322$62bc8a00$(E-Mail Removed)>...
> > 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.