Re: Checking for a named member of a collection?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: onedaywhen (onedaywhen_at_fmail.co.uk)
Date: 04/16/04


Date: 16 Apr 2004 04:07:39 -0700

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." <anonymous@discussions.microsoft.com> wrote in message news:<1a57301c42322$62bc8a00$a601280a@phx.gbl>...
> 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.


Relevant Pages

  • Re: Checking for a named member of a collection?
    ... checking a collection class for the existence ... of a member is usually coding which left to the client to do. ... Set myObject = myCollectionObject ... On Error Goto 0 ...
    (microsoft.public.excel.charting)
  • Re: How do I add error control to this script?
    ... On Error Goto 0 ... ' Trap possible error. ... ' Check if user is a member of the group. ... & strUser " removed on " strName ...
    (microsoft.public.scripting.vbscript)
  • Re: Determining users group
    ... on error goto 0 ... > They are not a member of users first. ... > You need to loop through all the groups to be certain, ...
    (microsoft.public.access.security)