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 8 1351
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:
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
On 18/06/2008 in message
<D2**********************************@microsoft.co mMorten Wennevik [C#
MVP] wrote:
>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
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:
On 18/06/2008 in message
<D2**********************************@microsoft.co mMorten Wennevik [C#
MVP] wrote:
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
On 18/06/2008 in message
<75**********************************@microsoft.co mMorten Wennevik [C#
MVP] wrote:
>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.
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
On Wed, 18 Jun 2008 19:32:16 -0700 (PDT), Tanzen
<aa*********@gmail.comwrote:
>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.
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.
--
Göran Andersson
_____ http://www.guffa.com
On Jun 19, 3:07*am, Göran Andersson <gu...@guffa.comwrote:
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.
--
Göran Andersson
_____http://www.guffa.com
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: baustin75 |
last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie
only when debugging in php designer 2005
--------------------------------------------------------------------------------
...
|
by: Satish |
last post by:
Hi Friends
I am little confused about the shadows keyword in VB.NET
could anyone explain with an example about Shadows keyword
Many thanks
Satish
|
by: SStory |
last post by:
Here is the situation.
I want to display Icons, Type of file etc from a file extension.
Upon initial program load I may only need icons for certain files. But
other operations will require...
|
by: Dale Levesque |
last post by:
Does anybody know how to determine if a collection contains a specific
member but not by key value as performed by the contains method? I have a
collection of custom structures that I only want to...
|
by: Michael D. Ober |
last post by:
In the code below, the IComparator function is never called. What am I
missing?
Public Class ArchiveInfo
Implements System.IComparable(Of ArchiveInfo)
Public FullName As String = ""
Public...
|
by: BSamp |
last post by:
I have the following business entity classes shown below. I have a data
layer that
retrieves the data from the database and populates a new instance of
the PendingRecord class then adds it to the...
|
by: Hexman |
last post by:
Hello All,
I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm
using some VB6 code, .Net2003 code,...
|
by: dmstn |
last post by:
Okay. I'm a new user here. I want some help. I have an assignment to do. I'm good so far but I miss something. Okay:
We have to create a public class named "Seizure". This class contains a public...
|
by: hcaptech |
last post by:
This is my Test.can you help me ?
1.Which of the following statement about C# varialble is incorrect ?
A.A variable is a computer memory location identified by a unique name
B.A variable's name...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |