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