PC Review


Reply
Thread Tools Rate Thread

Checking for a named member of a collection?

 
 
Jim S.
Guest
Posts: n/a
 
      15th Apr 2004
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.
 
Reply With Quote
 
 
 
 
onedaywhen
Guest
Posts: n/a
 
      16th Apr 2004
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.

 
Reply With Quote
 
Tushar Mehta
Guest
Posts: n/a
 
      19th Apr 2004
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

In article <1a57301c42322$62bc8a00$(E-Mail Removed)>,
(E-Mail Removed) says...
> 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
>
>

 
Reply With Quote
 
onedaywhen
Guest
Posts: n/a
 
      19th Apr 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>

--

(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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Setting a subform's SourceObject to a member of a collection Dennis Snelgrove Microsoft Access Form Coding 0 11th Jan 2011 02:41 AM
Setting a subform to a member of a collection Dennis Snelgrove Microsoft Access VBA Modules 2 3rd Jun 2010 06:53 PM
Referring to a member of the workbooks collection by name. Andrew Microsoft Excel Programming 5 11th May 2007 11:34 AM
member in collection =?Utf-8?B?WmVzdDRDc2hhcnA=?= Microsoft C# .NET 1 7th Jul 2005 07:33 AM
Adding member to an SMS collection Christoph Duesmann Microsoft VB .NET 0 21st Jul 2004 08:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:52 AM.