Contains

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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
 
Kevin said:
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.
 
Back
Top