Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Help with the collection contains method

Reply
 
Old 06-18-2008   #1 (permalink)
Tanzen


 
 

Help with the collection contains method

I'm working in visual studio 2005 trying to learn visual basic. Having
come from an VB for Access background, I'm finding it a big learning
curve. I have been working through several e-books which have shown
how to use custom collections to store lists of data. I've coded the
custom collection and before I add members to it, I want to use the
contains method to ensure the data does not already exist in the
collection. My play code is below.

Basically I have a form with a list box and a button. When you click
the button it is supposed to add a string value "Spot" to the dog
collection and then show that value in the list box. But it should
only add the item if the dog collection does not already contain
"Spot". I've highlighted the problem area below and all the project
code. The error I get when I try the code below is "Value of type
'String' can not be converted to 'basicCollections.Dog".

What I don't understand is why a string value would be unacceptable in
the contain method when the dog collection collects string data? Could
someone help explain what I am doing wrong?

Thank you,

'----------------------------- My Dog Collection Test
------------------------------

Public Class Form1
Private dog_list As New DogCollection

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

'<<<<<<<<<<<<<< ERROR >>>>>>>>>>>>>>>>>>
If Not dog_list.Contains("Spot") Then
' Value of type 'String' can not be converted to
'basicCollections.Dog'
'<<<<<<<<<<<<<< ERROR >>>>>>>>>>>>>>>>>>
dog_list.Add(New Dog("Spot"))
End If

For Each dog As Dog In dog_list
' Add Collection members to form list box
Me.ListBox1.Items.Add(dog.ToString)


Next dog

End Sub
End Class

Public Class Dog
Private m_Dog As String

Public Sub New(ByVal first_name As String)
m_Dog = first_name

End Sub
Public Overrides Function ToString() As String
Return m_Dog
End Function
End Class

' A strongly typed collection of Dogs.
Public Class DogCollection
Inherits CollectionBase

' Add an Dog.
Public Sub Add(ByVal value As Dog)
List.Add(value)
End Sub

' Return True if the collection contains this Dog.
Public Function Contains(ByVal value As Dog) As Boolean
Return List.Contains(value)
End Function

' Return this Dog's index.
Public Function IndexOf(ByVal value As Dog) As Integer
Return List.IndexOf(value)
End Function

' Return the Dog at this position.
Default Public ReadOnly Property Item(ByVal index As Integer) As
Dog
Get
Return DirectCast(List.Item(index), Dog)
End Get
End Property


End Class

My System SpecsSystem Spec
Old 06-18-2008   #2 (permalink)
Morten Wennevik [C# MVP]


 
 

RE: Help with the collection contains method

Hi Tanzen,

The Contains method expects to be passed a Dog object. Either change the
line to

If Not dog_list.Contains(New Dog("Spot"))

or add a second Contains method that uses a string parameter

Public Function Contains(ByVal firstName As String) As Boolean
For Each dog As Dog In List
If dog.ToString() = firstName Then
Return True
End If
Next

Return False
End Function

--
Happy Coding!
Morten Wennevik [C# MVP]


"Tanzen" wrote:
Quote:

> I'm working in visual studio 2005 trying to learn visual basic. Having
> come from an VB for Access background, I'm finding it a big learning
> curve. I have been working through several e-books which have shown
> how to use custom collections to store lists of data. I've coded the
> custom collection and before I add members to it, I want to use the
> contains method to ensure the data does not already exist in the
> collection. My play code is below.
>
> Basically I have a form with a list box and a button. When you click
> the button it is supposed to add a string value "Spot" to the dog
> collection and then show that value in the list box. But it should
> only add the item if the dog collection does not already contain
> "Spot". I've highlighted the problem area below and all the project
> code. The error I get when I try the code below is "Value of type
> 'String' can not be converted to 'basicCollections.Dog".
>
> What I don't understand is why a string value would be unacceptable in
> the contain method when the dog collection collects string data? Could
> someone help explain what I am doing wrong?
>
> Thank you,
>
> '----------------------------- My Dog Collection Test
> ------------------------------
>
> Public Class Form1
> Private dog_list As New DogCollection
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) Handles Button1.Click
>
> '<<<<<<<<<<<<<< ERROR >>>>>>>>>>>>>>>>>>
> If Not dog_list.Contains("Spot") Then
> ' Value of type 'String' can not be converted to
> 'basicCollections.Dog'
> '<<<<<<<<<<<<<< ERROR >>>>>>>>>>>>>>>>>>
> dog_list.Add(New Dog("Spot"))
> End If
>
> For Each dog As Dog In dog_list
> ' Add Collection members to form list box
> Me.ListBox1.Items.Add(dog.ToString)
>
>
> Next dog
>
> End Sub
> End Class
>
> Public Class Dog
> Private m_Dog As String
>
> Public Sub New(ByVal first_name As String)
> m_Dog = first_name
>
> End Sub
> Public Overrides Function ToString() As String
> Return m_Dog
> End Function
> End Class
>
> ' A strongly typed collection of Dogs.
> Public Class DogCollection
> Inherits CollectionBase
>
> ' Add an Dog.
> Public Sub Add(ByVal value As Dog)
> List.Add(value)
> End Sub
>
> ' Return True if the collection contains this Dog.
> Public Function Contains(ByVal value As Dog) As Boolean
> Return List.Contains(value)
> End Function
>
> ' Return this Dog's index.
> Public Function IndexOf(ByVal value As Dog) As Integer
> Return List.IndexOf(value)
> End Function
>
> ' Return the Dog at this position.
> Default Public ReadOnly Property Item(ByVal index As Integer) As
> Dog
> Get
> Return DirectCast(List.Item(index), Dog)
> End Get
> End Property
>
>
> End Class
>
My System SpecsSystem Spec
Old 06-18-2008   #3 (permalink)
Jeff Gaines


 
 

Re: Help with the collection contains method

On 18/06/2008 in message
<D2B11613-DA99-4913-8C60-09D1EB575928@xxxxxx> Morten Wennevik [C#
MVP] wrote:
Quote:

>The Contains method expects to be passed a Dog object. Either change the
>line to
>
>If Not dog_list.Contains(New Dog("Spot"))
>
>or add a second Contains method that uses a string parameter
>
> Public Function Contains(ByVal firstName As String) As Boolean
> For Each dog As Dog In List
> If dog.ToString() = firstName Then
> Return True
> End If
> Next
>
> Return False
> End Function
Can I chime in with a question please?

If the dog_list already contains a Dog object where the name is Spot will
it be regarded as equal to a new Dog object where the name is also Spot?
It seems to me they are different objects.

I use your second technique in several of my apps which works fine for me.

--
Jeff Gaines Damerham Hampshire UK
The facts, although interesting, are irrelevant
My System SpecsSystem Spec
Old 06-18-2008   #4 (permalink)
Morten Wennevik [C# MVP]


 
 

Re: Help with the collection contains method

Hi Jeff,

The first method will consider two dogs to be different even if they are
called the same. The second method will consider two dogs to be the same if
they have the same name.

It is possible to get the first method to consider two dog objects to be
equal if they have the same name by overriding the Equals method in the dog
class. Remember to override GetHashCode as well if you override Equals. If
two objects are equal, they should generate the same hash code.

--
Happy Coding!
Morten Wennevik [C# MVP]


"Jeff Gaines" wrote:
Quote:

> On 18/06/2008 in message
> <D2B11613-DA99-4913-8C60-09D1EB575928@xxxxxx> Morten Wennevik [C#
> MVP] wrote:
>
Quote:

> >The Contains method expects to be passed a Dog object. Either change the
> >line to
> >
> >If Not dog_list.Contains(New Dog("Spot"))
> >
> >or add a second Contains method that uses a string parameter
> >
> > Public Function Contains(ByVal firstName As String) As Boolean
> > For Each dog As Dog In List
> > If dog.ToString() = firstName Then
> > Return True
> > End If
> > Next
> >
> > Return False
> > End Function
>
> Can I chime in with a question please?
>
> If the dog_list already contains a Dog object where the name is Spot will
> it be regarded as equal to a new Dog object where the name is also Spot?
> It seems to me they are different objects.
>
> I use your second technique in several of my apps which works fine for me.
>
> --
> Jeff Gaines Damerham Hampshire UK
> The facts, although interesting, are irrelevant
>
My System SpecsSystem Spec
Old 06-18-2008   #5 (permalink)
Jeff Gaines


 
 

Re: Help with the collection contains method

On 18/06/2008 in message
<75C75A4E-23D1-4733-B9C5-74BA38829BC5@xxxxxx> Morten Wennevik [C#
MVP] wrote:
Quote:

>It is possible to get the first method to consider two dog objects to be
>equal if they have the same name by overriding the Equals method in the dog
>class. Remember to override GetHashCode as well if you override Equals.
>If
>two objects are equal, they should generate the same hash code.
Will try that, thanks Morten :-)

--
Jeff Gaines Damerham Hampshire UK
I can please only one person per day. Today is not your day.
Tomorrow, isn't looking good either.
My System SpecsSystem Spec
Old 06-18-2008   #6 (permalink)
Tanzen


 
 

Re: Help with the collection contains method

You guys are fantastic. The first method provided by Morten did indeed
cause two separate "instances" with the name spot. So I used the
second method and it works perfectly. I would like to thank both you
gentlemen for the input as I was very much stuck.

If I may ask, would either of you have suggestions as to good resource
material to use on visual basic 2005/2008 .dotnet? I'm currently
subscribed to the Wrox training online library and reading everything
I can find on vb 2005. However, while they often give a cursory look
at the various methods capable in collections, hashtables and such,
they don't go into detail on each one. So while they may describe
working with add, remove, and so on, all I can find is usually a list
of other possible methods with only cursory recommendations on how
they might be used but without examples of their use to provide the
full syntax that is desperately needed for neophytes like me. Any
recommendations you may have on training material would be
appreciated.

I want to thank you again!

Aaron

My System SpecsSystem Spec
Old 06-19-2008   #7 (permalink)
Jack Jackson


 
 

Re: Help with the collection contains method

On Wed, 18 Jun 2008 19:32:16 -0700 (PDT), Tanzen
<aaron.nasby@xxxxxx> wrote:
Quote:

>You guys are fantastic. The first method provided by Morten did indeed
>cause two separate "instances" with the name spot. So I used the
>second method and it works perfectly. I would like to thank both you
>gentlemen for the input as I was very much stuck.
>
>If I may ask, would either of you have suggestions as to good resource
>material to use on visual basic 2005/2008 .dotnet? I'm currently
>subscribed to the Wrox training online library and reading everything
>I can find on vb 2005. However, while they often give a cursory look
>at the various methods capable in collections, hashtables and such,
>they don't go into detail on each one. So while they may describe
>working with add, remove, and so on, all I can find is usually a list
>of other possible methods with only cursory recommendations on how
>they might be used but without examples of their use to provide the
>full syntax that is desperately needed for neophytes like me. Any
>recommendations you may have on training material would be
>appreciated.
>
>I want to thank you again!
>
>Aaron
I make heavy use of Google. You have to be careful because you will
find some obsolete and just plain wrong stuff, but you often can find
useful examples.
My System SpecsSystem Spec
Old 06-19-2008   #8 (permalink)
Göran Andersson


 
 

Re: Help with the collection contains method

Tanzen wrote:

8<
Quote:

> The error I get when I try the code below is "Value of type
> 'String' can not be converted to 'basicCollections.Dog".
>
> What I don't understand is why a string value would be unacceptable in
> the contain method when the dog collection collects string data? Could
> someone help explain what I am doing wrong?
No, the collection doesn't contain string data, it contains instanced of
the Dog class.

In your Dog class you need a method that compares the dog to a string.
Then you can overload the Contains method in the collection with a
method that takes a string and loops through the list to check for a Dog
intance where the comparison method returns true.

--
Göran Andersson
_____
http://www.guffa.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Civil War Collection Gaming
object not a collection VB Script
Method invocation failed because [System.String] doesn't contain a method PowerShell
Music Collection Vista music pictures video
Do You have any HDCD's in Your CD-collection? Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46