|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Containsthe 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 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 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 Kevin Burton wrote:
> myObject.Contains(key) There are lots of much better collection objects available in the > 'Contains' is not a member of 'Microsoft.VisualBasic.Collection'. 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. |
|||||||||||||||||||||||