Help with the collection contains method

T

Tanzen

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
 
M

Morten Wennevik [C# MVP]

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
 
J

Jeff Gaines

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

Morten Wennevik [C# MVP]

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

Jeff Gaines

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 :)
 
T

Tanzen

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
 
J

Jack Jackson

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

Göran Andersson

Tanzen wrote:

8<
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.
 
T

Tanzen

Tanzen wrote:

8<



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.

That sounds exactly what I'm looking for. My question would be how do
you write the method that takes a string value and returns the index
of the collection? I have the method that returns the value if you
have the index, the problem is, I can't figure out the reverse lookup.

Thanks in advance!

Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top