Home All Groups Group Topic Archive Search About
Author
8 Jun 2006 3:06 PM
Kevin Burton
I have a Collection object and I would like to know if a particular key is in
the collection so naturally I try something like:

myObject.Contains(key)

But when I compile the project I get:

'Contains' is not a member of 'Microsoft.VisualBasic.Collection'.

I am running VS 2003. Is 'Contains' a new addition or am I just not calling
it right? If it is truly not available does someone have a workaround. Right
now I do something like:

myObject.Item(key)

which throws an exception when key is not in the collection. I would like a
friendlier response than that.

Thank you.

Kevin

Author
8 Jun 2006 3:54 PM
Claes Bergefall
Contains is new in VS2005 actually. You didn't say what type of objects you
store in your collection. If they are strings you can use
NameValueCollection instead. Other options are HashTable or deriving a new
class from NameObjectCollectionBase.

    /claes


Show quoteHide quote
"Kevin Burton" <KevinBur***@discussions.microsoft.com> wrote in message
news:DBF0098B-0A92-473A-A948-F0882FA6CA6D@microsoft.com...
>I have a Collection object and I would like to know if a particular key is
>in
> the collection so naturally I try something like:
>
> myObject.Contains(key)
>
> But when I compile the project I get:
>
> 'Contains' is not a member of 'Microsoft.VisualBasic.Collection'.
>
> I am running VS 2003. Is 'Contains' a new addition or am I just not
> calling
> it right? If it is truly not available does someone have a workaround.
> Right
> now I do something like:
>
> myObject.Item(key)
>
> which throws an exception when key is not in the collection. I would like
> a
> friendlier response than that.
>
> Thank you.
>
> Kevin
Author
8 Jun 2006 3:56 PM
Kerry Moorman
Kevin,

The Collection class is a holdover from VB6, mainly used as an aid to
converting VB6 projects to VB.Net.

You are right that the Collection class does not have a Contains method. You
are also right that the way to determine if the collection contains an object
with a specific key is to try to retrieve the object using Item and catching
the exception.

You might want to look into using one of .Net's native collection classes,
such as ArrayList or HashTable. For keyed collections, HashTable is a good
starting point.

Kerry Moorman


Show quoteHide quote
"Kevin Burton" wrote:

> I have a Collection object and I would like to know if a particular key is in
> the collection so naturally I try something like:
>
> myObject.Contains(key)
>
> But when I compile the project I get:
>
> 'Contains' is not a member of 'Microsoft.VisualBasic.Collection'.
>
> I am running VS 2003. Is 'Contains' a new addition or am I just not calling
> it right? If it is truly not available does someone have a workaround. Right
> now I do something like:
>
> myObject.Item(key)
>
> which throws an exception when key is not in the collection. I would like a
> friendlier response than that.
>
> Thank you.
>
> Kevin
Author
9 Jun 2006 11:30 AM
Phill W.
Kevin Burton wrote:

> myObject.Contains(key)

> 'Contains' is not a member of 'Microsoft.VisualBasic.Collection'.

There are lots of much better collection objects available in the
Framework, all of which are way better than the MS.VB.Collection.

For unique keys (where the order /doesn't/ matter), I'd suggest a
HashTable.

HTH,
    Phill  W.