By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

HELP!! class that returns an arraylist

P: n/a
I have a class that returns an arraylist. How do I fill a list box from what
is returned? It returns customers which is a arraylist but I cant seem to
get the stuff to fill a list box. I just learning and really need some help
bad.
Public Shared Function GetAll() As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _
"FROM CustomerTable"

Try
Dim adpCustomer As New _
OleDbDataAdapter(sqlQuery, cnnCustomer)
adpCustomer.Fill(dsCustomer, "CustTable")
If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow
' Clear the array list

customers.Clear()
For Each dsRow In dsCustomer.Tables("CustTable").Rows
name = dsRow("Name")
address = dsRow("Address")
phoneno = dsRow("PhoneNo")
Dim aCustomer As New _
Customer(name, address, phoneno)
customers.Add(aCustomer)
Next

Else
' No records in database

End If
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
Throw New NotFoundException("Not Found")
End Try

Return customers
End Function
Nov 21 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
I might have missed it but I couldn't find where you dimensioned customers as
an array list like:
dim customers as arraylist = new arraylist

"Allen" wrote:
I have a class that returns an arraylist. How do I fill a list box from what
is returned? It returns customers which is a arraylist but I cant seem to
get the stuff to fill a list box. I just learning and really need some help
bad.
Public Shared Function GetAll() As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _
"FROM CustomerTable"

Try
Dim adpCustomer As New _
OleDbDataAdapter(sqlQuery, cnnCustomer)
adpCustomer.Fill(dsCustomer, "CustTable")
If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow
' Clear the array list

customers.Clear()
For Each dsRow In dsCustomer.Tables("CustTable").Rows
name = dsRow("Name")
address = dsRow("Address")
phoneno = dsRow("PhoneNo")
Dim aCustomer As New _
Customer(name, address, phoneno)
customers.Add(aCustomer)
Next

Else
' No records in database

End If
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
Throw New NotFoundException("Not Found")
End Try

Return customers
End Function

Nov 21 '05 #2

P: n/a
Dennis, Thanks for helping me. I'm calling the function from a form.
Here is what I have on the onLoad event on the form.
Dim Cust As Customer
Dim carr As ArrayList = New ArrayList
carr = Cust.GetAll
LstBoxCustomer.DataSource = carr

Dim x As Integer
x = carr.Count
Dim i As Integer
While i < carr.Count
LstBoxCustomer.Items.Add(carr.Item(i).ToString())
i += 1
End While
End Sub

I have been trying all kinds of things but nothing seems to work. The while
statement was my last attempt so I left everything in there for you to see
what I have been trying.

Allen

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
I might have missed it but I couldn't find where you dimensioned customers
as
an array list like:
dim customers as arraylist = new arraylist

"Allen" wrote:
I have a class that returns an arraylist. How do I fill a list box from
what
is returned? It returns customers which is a arraylist but I cant seem to
get the stuff to fill a list box. I just learning and really need some
help
bad.
Public Shared Function GetAll() As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _
"FROM CustomerTable"

Try
Dim adpCustomer As New _
OleDbDataAdapter(sqlQuery, cnnCustomer)
adpCustomer.Fill(dsCustomer, "CustTable")
If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow
' Clear the array list

customers.Clear()
For Each dsRow In dsCustomer.Tables("CustTable").Rows
name = dsRow("Name")
address = dsRow("Address")
phoneno = dsRow("PhoneNo")
Dim aCustomer As New _
Customer(name, address, phoneno)
customers.Add(aCustomer)
Next

Else
' No records in database

End If
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
Throw New NotFoundException("Not Found")
End Try

Return customers
End Function

Nov 21 '05 #3

P: n/a
Allen,

Are you the one which has an instructor who told that you should use this
crazy construction.
I assume you are, therefore I answer for you only the technical error.

In a listbox you can use a datasource or fill the items, a kind of two
possibilities in one control, however not combined. So the least you have to
do is *not* setting the datasource. A little bit about your code, use the
for statement that is much easier (I show you code for 1.1 in 1.0 it is a
little bit different tell that than).

\\\
For i as Integer = 0 to carr.Count -1
LstBoxCustomer.Items.Add(carr.Item(i).ToString())
Next
///

I hope this helps?

Cor
Nov 21 '05 #4

P: n/a
Hi,

When you databind an arraylist to a listbox it will only display the
properties contained in the class its public variables wont work. Second
you need to set the displaymember for the listbox here is a quick example.
Dim arBook As ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim cBook As Book

Dim x As Integer

arBook = New ArrayList

For x = 1 To 10

cBook = New Book

cBook.Name = "Book " & x.ToString

cBook.ID = x

arBook.Add(cBook)

Next

ListBox1.DataSource = arBook

ListBox1.DisplayMember = "Name"

End Sub
The class
Public Class Book

Private m_name As String

Dim m_ID As String

Public Property Name() As String

Get

Return m_name

End Get

Set(ByVal Value As String)

m_name = Value

End Set

End Property

Public Property ID() As String

Get

Return m_ID

End Get

Set(ByVal Value As String)

m_ID = Value

End Set

End Property

End Class

Ken
-------------------------
"Allen" <me***@pacbell.net> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I have a class that returns an arraylist. How do I fill a list box from what
is returned? It returns customers which is a arraylist but I cant seem to
get the stuff to fill a list box. I just learning and really need some help
bad.
Public Shared Function GetAll() As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _
"FROM CustomerTable"

Try
Dim adpCustomer As New _
OleDbDataAdapter(sqlQuery, cnnCustomer)
adpCustomer.Fill(dsCustomer, "CustTable")
If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow
' Clear the array list

customers.Clear()
For Each dsRow In dsCustomer.Tables("CustTable").Rows
name = dsRow("Name")
address = dsRow("Address")
phoneno = dsRow("PhoneNo")
Dim aCustomer As New _
Customer(name, address, phoneno)
customers.Add(aCustomer)
Next

Else
' No records in database

End If
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
Throw New NotFoundException("Not Found")
End Try

Return customers
End Function

Nov 21 '05 #5

P: n/a
I really hate the way we are having to do this.
Now that I can get the list box to show the names I cant get the PhoneNumber
that is suppose to be in the array with everything else.
Here is how I got the list box to show the names.
Private Sub Find_Customer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

arCust = New ArrayList

arCust = cCustomer.GetAll

LstBoxCustomer.DataSource = arCust

LstBoxCustomer.DisplayMember = "CustomerName"
It shows the names but how do I get the other parts of the array so I can
pass it to a find fucntion that uses the phone number as a key. The array is
suppose to have the name, address and phone number in it.

I tried to set a var called Key to the itemSelected like this'

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFind.Click

key = LstBoxCustomer.SelectedItem()

MsgBox(key)

End Sub

but I get this error "Cast from type 'Customer' to type 'String' is not
valid.

Thanks

Allen

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

When you databind an arraylist to a listbox it will only display
the
properties contained in the class its public variables wont work. Second
you need to set the displaymember for the listbox here is a quick example.
Dim arBook As ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim cBook As Book

Dim x As Integer

arBook = New ArrayList

For x = 1 To 10

cBook = New Book

cBook.Name = "Book " & x.ToString

cBook.ID = x

arBook.Add(cBook)

Next

ListBox1.DataSource = arBook

ListBox1.DisplayMember = "Name"

End Sub
The class
Public Class Book

Private m_name As String

Dim m_ID As String

Public Property Name() As String

Get

Return m_name

End Get

Set(ByVal Value As String)

m_name = Value

End Set

End Property

Public Property ID() As String

Get

Return m_ID

End Get

Set(ByVal Value As String)

m_ID = Value

End Set

End Property

End Class

Ken
-------------------------
"Allen" <me***@pacbell.net> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I have a class that returns an arraylist. How do I fill a list box from
what
is returned? It returns customers which is a arraylist but I cant seem to
get the stuff to fill a list box. I just learning and really need some
help
bad.
Public Shared Function GetAll() As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _
"FROM CustomerTable"

Try
Dim adpCustomer As New _
OleDbDataAdapter(sqlQuery, cnnCustomer)
adpCustomer.Fill(dsCustomer, "CustTable")
If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow
' Clear the array list

customers.Clear()
For Each dsRow In dsCustomer.Tables("CustTable").Rows
name = dsRow("Name")
address = dsRow("Address")
phoneno = dsRow("PhoneNo")
Dim aCustomer As New _
Customer(name, address, phoneno)
customers.Add(aCustomer)
Next

Else
' No records in database

End If
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
Throw New NotFoundException("Not Found")
End Try

Return customers
End Function

Nov 21 '05 #6

P: n/a
Allen,
key = LstBoxCustomer.SelectedItem()
MsgBox(key)

but I get this error "Cast from type 'Customer' to type 'String' is not
valid. The "key" variable has a Customer object in it, use one of Customer's
properties to access the info you want:

MessageBox.Show(key.CustomerName)
MessageBox.Show(key.PhoneNumber)
MessageBox.Show(key.Address)

Hope this helps
Jay

"Allen" <me***@pacbell.net> wrote in message
news:Ob***************@TK2MSFTNGP15.phx.gbl...I really hate the way we are having to do this.
Now that I can get the list box to show the names I cant get the
PhoneNumber
that is suppose to be in the array with everything else.
Here is how I got the list box to show the names.
Private Sub Find_Customer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

arCust = New ArrayList

arCust = cCustomer.GetAll

LstBoxCustomer.DataSource = arCust

LstBoxCustomer.DisplayMember = "CustomerName"
It shows the names but how do I get the other parts of the array so I can
pass it to a find fucntion that uses the phone number as a key. The array
is
suppose to have the name, address and phone number in it.

I tried to set a var called Key to the itemSelected like this'

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFind.Click

key = LstBoxCustomer.SelectedItem()

MsgBox(key)

End Sub

but I get this error "Cast from type 'Customer' to type 'String' is not
valid.

Thanks

Allen

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

When you databind an arraylist to a listbox it will only display
the
properties contained in the class its public variables wont work. Second
you need to set the displaymember for the listbox here is a quick
example.
Dim arBook As ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim cBook As Book

Dim x As Integer

arBook = New ArrayList

For x = 1 To 10

cBook = New Book

cBook.Name = "Book " & x.ToString

cBook.ID = x

arBook.Add(cBook)

Next

ListBox1.DataSource = arBook

ListBox1.DisplayMember = "Name"

End Sub
The class
Public Class Book

Private m_name As String

Dim m_ID As String

Public Property Name() As String

Get

Return m_name

End Get

Set(ByVal Value As String)

m_name = Value

End Set

End Property

Public Property ID() As String

Get

Return m_ID

End Get

Set(ByVal Value As String)

m_ID = Value

End Set

End Property

End Class

Ken
-------------------------
"Allen" <me***@pacbell.net> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I have a class that returns an arraylist. How do I fill a list box from
what
is returned? It returns customers which is a arraylist but I cant seem to
get the stuff to fill a list box. I just learning and really need some
help
bad.
Public Shared Function GetAll() As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _
"FROM CustomerTable"

Try
Dim adpCustomer As New _
OleDbDataAdapter(sqlQuery, cnnCustomer)
adpCustomer.Fill(dsCustomer, "CustTable")
If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow
' Clear the array list

customers.Clear()
For Each dsRow In dsCustomer.Tables("CustTable").Rows
name = dsRow("Name")
address = dsRow("Address")
phoneno = dsRow("PhoneNo")
Dim aCustomer As New _
Customer(name, address, phoneno)
customers.Add(aCustomer)
Next

Else
' No records in database

End If
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
Throw New NotFoundException("Not Found")
End Try

Return customers
End Function


Nov 21 '05 #7

P: n/a
I am really begining to hate programming right now.
I cant get my list box to display the right information.
I have a class Customers that has a function in it called GetAll
Public Shared Function GetAll() As ArrayList
Return CustomerDA.GetAll
End Function

That calls CustomerDA.GetAll

' GetAll Method

Public Shared Function GetAll() As ArrayListDim dsCustomer As New DataSet()

Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _

"FROM CustomerTable"

Try

Dim adpCustomer As New _

OleDbDataAdapter(sqlQuery, cnnCustomer)

adpCustomer.Fill(dsCustomer, "CustTable")

If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow

' Clear the array list

customers.Clear()

For Each dsRow In dsCustomer.Tables("CustTable").Rows

name = dsRow("Name")

address = dsRow("Address")

phoneno = dsRow("PhoneNo")

Dim aCustomer As New _

Customer(name, address, phoneno)

customers.Add(aCustomer)

Next

Else

' No records in database

End If

dsCustomer = Nothing

Catch e As Exception

Console.WriteLine(e.ToString)

Throw New NotFoundException("Not Found")

End Try

Return customers

End Function

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Allen,
key = LstBoxCustomer.SelectedItem()
MsgBox(key)

but I get this error "Cast from type 'Customer' to type 'String' is not
valid.

The "key" variable has a Customer object in it, use one of Customer's
properties to access the info you want:

MessageBox.Show(key.CustomerName)
MessageBox.Show(key.PhoneNumber)
MessageBox.Show(key.Address)

Hope this helps
Jay

"Allen" <me***@pacbell.net> wrote in message
news:Ob***************@TK2MSFTNGP15.phx.gbl...
I really hate the way we are having to do this.
Now that I can get the list box to show the names I cant get the
PhoneNumber
that is suppose to be in the array with everything else.
Here is how I got the list box to show the names.
Private Sub Find_Customer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

arCust = New ArrayList

arCust = cCustomer.GetAll

LstBoxCustomer.DataSource = arCust

LstBoxCustomer.DisplayMember = "CustomerName"
It shows the names but how do I get the other parts of the array so I can
pass it to a find fucntion that uses the phone number as a key. The array
is
suppose to have the name, address and phone number in it.

I tried to set a var called Key to the itemSelected like this'

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFind.Click

key = LstBoxCustomer.SelectedItem()

MsgBox(key)

End Sub

but I get this error "Cast from type 'Customer' to type 'String' is not
valid.

Thanks

Allen

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

When you databind an arraylist to a listbox it will only display
the
properties contained in the class its public variables wont work.
Second
you need to set the displaymember for the listbox here is a quick
example.
Dim arBook As ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim cBook As Book

Dim x As Integer

arBook = New ArrayList

For x = 1 To 10

cBook = New Book

cBook.Name = "Book " & x.ToString

cBook.ID = x

arBook.Add(cBook)

Next

ListBox1.DataSource = arBook

ListBox1.DisplayMember = "Name"

End Sub
The class
Public Class Book

Private m_name As String

Dim m_ID As String

Public Property Name() As String

Get

Return m_name

End Get

Set(ByVal Value As String)

m_name = Value

End Set

End Property

Public Property ID() As String

Get

Return m_ID

End Get

Set(ByVal Value As String)

m_ID = Value

End Set

End Property

End Class

Ken
-------------------------
"Allen" <me***@pacbell.net> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
I have a class that returns an arraylist. How do I fill a list box from
what
is returned? It returns customers which is a arraylist but I cant seem
to
get the stuff to fill a list box. I just learning and really need some
help
bad.
Public Shared Function GetAll() As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _
"FROM CustomerTable"

Try
Dim adpCustomer As New _
OleDbDataAdapter(sqlQuery, cnnCustomer)
adpCustomer.Fill(dsCustomer, "CustTable")
If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow
' Clear the array list

customers.Clear()
For Each dsRow In dsCustomer.Tables("CustTable").Rows
name = dsRow("Name")
address = dsRow("Address")
phoneno = dsRow("PhoneNo")
Dim aCustomer As New _
Customer(name, address, phoneno)
customers.Add(aCustomer)
Next

Else
' No records in database

End If
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
Throw New NotFoundException("Not Found")
End Try

Return customers
End Function



Nov 21 '05 #8

P: n/a
Please do not crosspost .NET questions to non-.NET groups. The *.vb.* groups
are for VB6 and earlier. The .NET groups have dotnet in their names.
Nov 21 '05 #9

P: n/a
Allen wrote:
I have attached the code for the two classes I have to get this done


1. Don't attach code. In fact, don't attach anything.

2. Some of the groups that you sent this to, namely,
microsoft.public.vb.control, microsoft.public.vb.controls.creation and
microsoft.public.vb.controls.databound, are all VB Classic groups, not
DotNet groups. Watch where you post to.
--
Regards,

Michael Cole
Nov 21 '05 #10

P: n/a
sorry about that.
I didnt notice that the others were not .net newsgroups.
Any ideas on my problem?

Allen
"Michael Cole" <no***@hansen.com> wrote in message
news:ec**************@tk2msftngp13.phx.gbl...
Allen wrote:
I have attached the code for the two classes I have to get this done


1. Don't attach code. In fact, don't attach anything.

2. Some of the groups that you sent this to, namely,
microsoft.public.vb.control, microsoft.public.vb.controls.creation and
microsoft.public.vb.controls.databound, are all VB Classic groups, not
DotNet groups. Watch where you post to.
--
Regards,

Michael Cole

Nov 21 '05 #11

P: n/a
Allen,
When posting code its "better" to put the code in a Zip file, as .vb files
are normally blocked to ensure you are not attempting to spread a virus. Or
inline like you did here...

Its also "better" to stay in the newsgroup you started, rather then splatter
your message on a number of VB6 newsgroups. Once you have "exhausted" this
group, then I would recommend turning to other .NET groups...

However!

Four comments on your Customer class:

1) Rather then use "Sub Initialize", I would recommend using "Shared Sub
New", as it is called automatically for you. (You don't need to remember to
explicitly call it).
2) Rather then use comments, I would recommend using Regions to delimit
blocks of code.
3) Your class should use Properties instead of accessor methods (GetName,
SetName, GetAddress, SetAddress, GetPhoneNo, and SetPhoneNo). As the data
binding requires Properties.
4) .NET has a predefined "TellAboutSelf" function, its called ToString, you
should override ToString instead of defining your own function.

So your class becomes:

Public Class Customer

#Region " Attributes "

Private m_name As String
Private m_address As String
Private m_phoneNo As String

#End Region

#Region " Data Access Shared Methods "

Shared Sub New()
' TODO: rename CustomerDA.Initialize to New!
' allowing us to remove shared Customer.New...
CustomerDA.Initialize()
End Sub

Public Shared Function Find(ByVal PhoneNo As String) As Customer
Return CustomerDA.Find(PhoneNo)
End Function

Public Shared Function GetAll() As ArrayList
Return CustomerDA.GetAll
End Function

Public Shared Sub Terminate()
CustomerDA.Terminate()
End Sub

#End Region

#Region " Data Access Instance Methods "

Public Sub AddNew()
CustomerDA.AddNew(Me)
End Sub

Public Sub Update()
CustomerDA.Update(Me)
End Sub

Public Sub Delete()
CustomerDA.Delete(Me)
End Sub

#End Region

#Region " Properties "

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property

Public Property Address() As String
Get
Return m_address
End Get
Set(ByVal value As String)
m_address = value
End Set
End Property

Public Property PhoneNo() As String
Get
Return m_phoneNo
End Get
Set(ByVal value As String)
m_phoneNo = value
End Set
End Property

#End Region

#Region " Tell About Self "

Public Overrides Function ToString() As String
Dim info As String
info = "Name = " & Name & _
", Address = " & Address & _
", Phone No = " & PhoneNo
Return info
End Function

#End Region

#Region " Constructors "

'default constructor
Public Sub New()
End Sub

'constructor (3 parameters)
Public Sub New(ByVal aName As String, ByVal anAddress As String, _
ByVal aPhoneNo As String)
m_name = aName
m_address = anAddress
m_phoneNo = aPhoneNo
End Sub

#End Region

End Class

Then as Ken stated when you bind to the ListBox, you would use something
like:

Dim list As ArrayList
Dim ListBox1 As ListBox

ListBox1.DataSource = list
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "PhoneNo"

To get the selected customer from the ListBox you can use the
ListBox.SelectedItem property:
Dim selectedCustomer As Customer =
DirectCast(ListBox1.SelectedItem, Customer)

To get the selected phone number from the ListBox you can use the
ListBox.SelectedValue property:
Dim phoneNo As String = DirectCast(ListBox1.SelectedValue,
String)

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I am really begining to hate programming.
I have to have this thing done for class and cant seem to get it to do
what
its supose to do.
I have two classes one Customers and one CustomersDA
I have a form that is supose to display customer names in a listbox and
then
have 3 buttons (find, Update and Close) I cant seem to get things to work
the right way. Everyone has had good input but its not helped at all.

I have attached the code for the two classes I have to get this done for
class its making me get behind. If someone out there can please take a
look
at it and help me out. I have tried it all and just cant get it. Once I
see
how it is done it will help me to understand. I really need help bad. I
have
looked for days on how to do it.
Code is pasted below. How do you f'n do this.
Thanks Allen

<<snip>>
Nov 21 '05 #12

P: n/a
Thanks for the info but it still is not working write. I cant change the
class code it came from my instructor and we have to work with it.
Here is what I am doing on a forms onload event

Dim cCustomer As New Customer
Dim arCust As New ArrayList
Dim key As String
Private Sub Find_Customer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim lst As New ArrayList
lst = cCustomer.GetAll()
LstBoxCustomer.DataSource = lst
LstBoxCustomer.DisplayMember = "Name"
LstBoxCustomer.ValueMember = "phoneNo"
and I get this error : Could not bind to the new display member.
In the debug window I get this.

- lst {Length=5} System.Collections.ArrayList
+ (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (1) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (2) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (3) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (4) {WindowsApplication2.Customer} WindowsApplication2.Customer

when I expand each one I get this.

- lst {Length=5} System.Collections.ArrayList
- (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
address "Atlanta" String
CustomerName "Eleanor" String
name "Eleanor" String
phoneNo "123-4567" String

Any help Jay would be great. It should not be that hard to fill a listbox
with name and then use a find button to find the phone number and address to
be put into textboxes. But we have to use what he gave us. datasets would be
easier.

Thanks
Allen
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Allen,
When posting code its "better" to put the code in a Zip file, as .vb files
are normally blocked to ensure you are not attempting to spread a virus.
Or
inline like you did here...

Its also "better" to stay in the newsgroup you started, rather then
splatter
your message on a number of VB6 newsgroups. Once you have "exhausted" this
group, then I would recommend turning to other .NET groups...

However!

Four comments on your Customer class:

1) Rather then use "Sub Initialize", I would recommend using "Shared Sub
New", as it is called automatically for you. (You don't need to remember
to
explicitly call it).
2) Rather then use comments, I would recommend using Regions to delimit
blocks of code.
3) Your class should use Properties instead of accessor methods (GetName,
SetName, GetAddress, SetAddress, GetPhoneNo, and SetPhoneNo). As the data
binding requires Properties.
4) .NET has a predefined "TellAboutSelf" function, its called ToString,
you
should override ToString instead of defining your own function.

So your class becomes:

Public Class Customer

#Region " Attributes "

Private m_name As String
Private m_address As String
Private m_phoneNo As String

#End Region

#Region " Data Access Shared Methods "

Shared Sub New()
' TODO: rename CustomerDA.Initialize to New!
' allowing us to remove shared Customer.New...
CustomerDA.Initialize()
End Sub

Public Shared Function Find(ByVal PhoneNo As String) As Customer
Return CustomerDA.Find(PhoneNo)
End Function

Public Shared Function GetAll() As ArrayList
Return CustomerDA.GetAll
End Function

Public Shared Sub Terminate()
CustomerDA.Terminate()
End Sub

#End Region

#Region " Data Access Instance Methods "

Public Sub AddNew()
CustomerDA.AddNew(Me)
End Sub

Public Sub Update()
CustomerDA.Update(Me)
End Sub

Public Sub Delete()
CustomerDA.Delete(Me)
End Sub

#End Region

#Region " Properties "

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property

Public Property Address() As String
Get
Return m_address
End Get
Set(ByVal value As String)
m_address = value
End Set
End Property

Public Property PhoneNo() As String
Get
Return m_phoneNo
End Get
Set(ByVal value As String)
m_phoneNo = value
End Set
End Property

#End Region

#Region " Tell About Self "

Public Overrides Function ToString() As String
Dim info As String
info = "Name = " & Name & _
", Address = " & Address & _
", Phone No = " & PhoneNo
Return info
End Function

#End Region

#Region " Constructors "

'default constructor
Public Sub New()
End Sub

'constructor (3 parameters)
Public Sub New(ByVal aName As String, ByVal anAddress As String, _
ByVal aPhoneNo As String)
m_name = aName
m_address = anAddress
m_phoneNo = aPhoneNo
End Sub

#End Region

End Class

Then as Ken stated when you bind to the ListBox, you would use something
like:

Dim list As ArrayList
Dim ListBox1 As ListBox

ListBox1.DataSource = list
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "PhoneNo"

To get the selected customer from the ListBox you can use the
ListBox.SelectedItem property:
Dim selectedCustomer As Customer =
DirectCast(ListBox1.SelectedItem, Customer)

To get the selected phone number from the ListBox you can use the
ListBox.SelectedValue property:
Dim phoneNo As String = DirectCast(ListBox1.SelectedValue,
String)

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I am really begining to hate programming.
I have to have this thing done for class and cant seem to get it to do
what
its supose to do.
I have two classes one Customers and one CustomersDA
I have a form that is supose to display customer names in a listbox and
then
have 3 buttons (find, Update and Close) I cant seem to get things to work
the right way. Everyone has had good input but its not helped at all.

I have attached the code for the two classes I have to get this done for
class its making me get behind. If someone out there can please take a
look
at it and help me out. I have tried it all and just cant get it. Once I
see
how it is done it will help me to understand. I really need help bad. I
have
looked for days on how to do it.
Code is pasted below. How do you f'n do this.
Thanks Allen

<<snip>>

Nov 21 '05 #13

P: n/a
Allen,
Thanks for the info but it still is not working write. The sample I gave should work with very minor tweaking. Remember that the
property names need to match exactly...

There is one minor "quirk" in my code. You should set the DisplayMember &
ValueMember before you set the DataSource, otherwise the
ListBox.SelectedIndexChanged event can occur with "different" values for the
SelectedValue property (as ValueMember has not be set yet). Alternatively
you can check the type of SelectedValue before you use it.

I cant change the
class code it came from my instructor and we have to work with it. This is Homework? :-(

I hope you understand that I cannot do your homework for you. As you are
less likely to actually learn anything....

Again the sample I gave should work with minor tweaking.

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:e8**************@TK2MSFTNGP14.phx.gbl... Thanks for the info but it still is not working write. I cant change the
class code it came from my instructor and we have to work with it.
Here is what I am doing on a forms onload event

Dim cCustomer As New Customer
Dim arCust As New ArrayList
Dim key As String
Private Sub Find_Customer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim lst As New ArrayList
lst = cCustomer.GetAll()
LstBoxCustomer.DataSource = lst
LstBoxCustomer.DisplayMember = "Name"
LstBoxCustomer.ValueMember = "phoneNo"
and I get this error : Could not bind to the new display member.
In the debug window I get this.

- lst {Length=5} System.Collections.ArrayList
+ (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (1) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (2) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (3) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (4) {WindowsApplication2.Customer} WindowsApplication2.Customer

when I expand each one I get this.

- lst {Length=5} System.Collections.ArrayList
- (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
address "Atlanta" String
CustomerName "Eleanor" String
name "Eleanor" String
phoneNo "123-4567" String

Any help Jay would be great. It should not be that hard to fill a listbox
with name and then use a find button to find the phone number and address
to
be put into textboxes. But we have to use what he gave us. datasets would
be
easier.

Thanks
Allen
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Allen,
When posting code its "better" to put the code in a Zip file, as .vb
files
are normally blocked to ensure you are not attempting to spread a virus.
Or
inline like you did here...

Its also "better" to stay in the newsgroup you started, rather then
splatter
your message on a number of VB6 newsgroups. Once you have "exhausted"
this
group, then I would recommend turning to other .NET groups...

However!

Four comments on your Customer class:

1) Rather then use "Sub Initialize", I would recommend using "Shared Sub
New", as it is called automatically for you. (You don't need to remember
to
explicitly call it).
2) Rather then use comments, I would recommend using Regions to delimit
blocks of code.
3) Your class should use Properties instead of accessor methods (GetName,
SetName, GetAddress, SetAddress, GetPhoneNo, and SetPhoneNo). As the data
binding requires Properties.
4) .NET has a predefined "TellAboutSelf" function, its called ToString,
you
should override ToString instead of defining your own function.

So your class becomes:

Public Class Customer

#Region " Attributes "

Private m_name As String
Private m_address As String
Private m_phoneNo As String

#End Region

#Region " Data Access Shared Methods "

Shared Sub New()
' TODO: rename CustomerDA.Initialize to New!
' allowing us to remove shared Customer.New...
CustomerDA.Initialize()
End Sub

Public Shared Function Find(ByVal PhoneNo As String) As Customer
Return CustomerDA.Find(PhoneNo)
End Function

Public Shared Function GetAll() As ArrayList
Return CustomerDA.GetAll
End Function

Public Shared Sub Terminate()
CustomerDA.Terminate()
End Sub

#End Region

#Region " Data Access Instance Methods "

Public Sub AddNew()
CustomerDA.AddNew(Me)
End Sub

Public Sub Update()
CustomerDA.Update(Me)
End Sub

Public Sub Delete()
CustomerDA.Delete(Me)
End Sub

#End Region

#Region " Properties "

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property

Public Property Address() As String
Get
Return m_address
End Get
Set(ByVal value As String)
m_address = value
End Set
End Property

Public Property PhoneNo() As String
Get
Return m_phoneNo
End Get
Set(ByVal value As String)
m_phoneNo = value
End Set
End Property

#End Region

#Region " Tell About Self "

Public Overrides Function ToString() As String
Dim info As String
info = "Name = " & Name & _
", Address = " & Address & _
", Phone No = " & PhoneNo
Return info
End Function

#End Region

#Region " Constructors "

'default constructor
Public Sub New()
End Sub

'constructor (3 parameters)
Public Sub New(ByVal aName As String, ByVal anAddress As String, _
ByVal aPhoneNo As String)
m_name = aName
m_address = anAddress
m_phoneNo = aPhoneNo
End Sub

#End Region

End Class

Then as Ken stated when you bind to the ListBox, you would use something
like:

Dim list As ArrayList
Dim ListBox1 As ListBox

ListBox1.DataSource = list
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "PhoneNo"

To get the selected customer from the ListBox you can use the
ListBox.SelectedItem property:
Dim selectedCustomer As Customer =
DirectCast(ListBox1.SelectedItem, Customer)

To get the selected phone number from the ListBox you can use the
ListBox.SelectedValue property:
Dim phoneNo As String = DirectCast(ListBox1.SelectedValue,
String)

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I am really begining to hate programming.
I have to have this thing done for class and cant seem to get it to do
what
its supose to do.
I have two classes one Customers and one CustomersDA
I have a form that is supose to display customer names in a listbox and
then
have 3 buttons (find, Update and Close) I cant seem to get things to
work
the right way. Everyone has had good input but its not helped at all.

I have attached the code for the two classes I have to get this done for
class its making me get behind. If someone out there can please take a
look
at it and help me out. I have tried it all and just cant get it. Once I
see
how it is done it will help me to understand. I really need help bad. I
have
looked for days on how to do it.
Code is pasted below. How do you f'n do this.
Thanks Allen

<<snip>>


Nov 21 '05 #14

P: n/a
Thanks for your help I found that I did not have Customer.Initialize() In
the code. I still have a problem with getting the phone number out of the
array.
what is working.
Customer.Initialize()
customers.Add(New Customer)
LstBoxCustomer.DisplayMember = "CustomerName"
LstBoxCustomer.ValueMember = "phoneNo"
LstBoxCustomer.DataSource = customers

I have a new listbox on another form that needs to show the Phone numbers. I
thought I could just change the code to show the phone number but It doesnt
work.

Customer.Initialize()
customers = aCustomer.GetAll()
customers.Add(New Customer)
lstBoxPhone.DisplayMember = "phoneno"
lstBoxPhone.ValueMember = "phoneno"
lstBoxPhone.DataSource = customers

thanks
Allen

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e7**************@TK2MSFTNGP09.phx.gbl...
Allen,
Thanks for the info but it still is not working write.

The sample I gave should work with very minor tweaking. Remember that the
property names need to match exactly...

There is one minor "quirk" in my code. You should set the DisplayMember &
ValueMember before you set the DataSource, otherwise the
ListBox.SelectedIndexChanged event can occur with "different" values for
the
SelectedValue property (as ValueMember has not be set yet). Alternatively
you can check the type of SelectedValue before you use it.

I cant change the
class code it came from my instructor and we have to work with it.

This is Homework? :-(

I hope you understand that I cannot do your homework for you. As you are
less likely to actually learn anything....

Again the sample I gave should work with minor tweaking.

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:e8**************@TK2MSFTNGP14.phx.gbl...
Thanks for the info but it still is not working write. I cant change the
class code it came from my instructor and we have to work with it.
Here is what I am doing on a forms onload event

Dim cCustomer As New Customer
Dim arCust As New ArrayList
Dim key As String
Private Sub Find_Customer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim lst As New ArrayList
lst = cCustomer.GetAll()
LstBoxCustomer.DataSource = lst
LstBoxCustomer.DisplayMember = "Name"
LstBoxCustomer.ValueMember = "phoneNo"
and I get this error : Could not bind to the new display member.
In the debug window I get this.

- lst {Length=5} System.Collections.ArrayList
+ (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (1) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (2) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (3) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (4) {WindowsApplication2.Customer} WindowsApplication2.Customer

when I expand each one I get this.

- lst {Length=5} System.Collections.ArrayList
- (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
address "Atlanta" String
CustomerName "Eleanor" String
name "Eleanor" String
phoneNo "123-4567" String

Any help Jay would be great. It should not be that hard to fill a listbox
with name and then use a find button to find the phone number and address
to
be put into textboxes. But we have to use what he gave us. datasets would
be
easier.

Thanks
Allen
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Allen,
When posting code its "better" to put the code in a Zip file, as .vb
files
are normally blocked to ensure you are not attempting to spread a virus.
Or
inline like you did here...

Its also "better" to stay in the newsgroup you started, rather then
splatter
your message on a number of VB6 newsgroups. Once you have "exhausted"
this
group, then I would recommend turning to other .NET groups...

However!

Four comments on your Customer class:

1) Rather then use "Sub Initialize", I would recommend using "Shared Sub
New", as it is called automatically for you. (You don't need to remember
to
explicitly call it).
2) Rather then use comments, I would recommend using Regions to delimit
blocks of code.
3) Your class should use Properties instead of accessor methods
(GetName,
SetName, GetAddress, SetAddress, GetPhoneNo, and SetPhoneNo). As the
data
binding requires Properties.
4) .NET has a predefined "TellAboutSelf" function, its called ToString,
you
should override ToString instead of defining your own function.

So your class becomes:

Public Class Customer

#Region " Attributes "

Private m_name As String
Private m_address As String
Private m_phoneNo As String

#End Region

#Region " Data Access Shared Methods "

Shared Sub New()
' TODO: rename CustomerDA.Initialize to New!
' allowing us to remove shared Customer.New...
CustomerDA.Initialize()
End Sub

Public Shared Function Find(ByVal PhoneNo As String) As Customer
Return CustomerDA.Find(PhoneNo)
End Function

Public Shared Function GetAll() As ArrayList
Return CustomerDA.GetAll
End Function

Public Shared Sub Terminate()
CustomerDA.Terminate()
End Sub

#End Region

#Region " Data Access Instance Methods "

Public Sub AddNew()
CustomerDA.AddNew(Me)
End Sub

Public Sub Update()
CustomerDA.Update(Me)
End Sub

Public Sub Delete()
CustomerDA.Delete(Me)
End Sub

#End Region

#Region " Properties "

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property

Public Property Address() As String
Get
Return m_address
End Get
Set(ByVal value As String)
m_address = value
End Set
End Property

Public Property PhoneNo() As String
Get
Return m_phoneNo
End Get
Set(ByVal value As String)
m_phoneNo = value
End Set
End Property

#End Region

#Region " Tell About Self "

Public Overrides Function ToString() As String
Dim info As String
info = "Name = " & Name & _
", Address = " & Address & _
", Phone No = " & PhoneNo
Return info
End Function

#End Region

#Region " Constructors "

'default constructor
Public Sub New()
End Sub

'constructor (3 parameters)
Public Sub New(ByVal aName As String, ByVal anAddress As String,
_
ByVal aPhoneNo As String)
m_name = aName
m_address = anAddress
m_phoneNo = aPhoneNo
End Sub

#End Region

End Class

Then as Ken stated when you bind to the ListBox, you would use something
like:

Dim list As ArrayList
Dim ListBox1 As ListBox

ListBox1.DataSource = list
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "PhoneNo"

To get the selected customer from the ListBox you can use the
ListBox.SelectedItem property:
Dim selectedCustomer As Customer =
DirectCast(ListBox1.SelectedItem, Customer)

To get the selected phone number from the ListBox you can use the
ListBox.SelectedValue property:
Dim phoneNo As String = DirectCast(ListBox1.SelectedValue,
String)

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I am really begining to hate programming.
I have to have this thing done for class and cant seem to get it to do
what
its supose to do.
I have two classes one Customers and one CustomersDA
I have a form that is supose to display customer names in a listbox and
then
have 3 buttons (find, Update and Close) I cant seem to get things to
work
the right way. Everyone has had good input but its not helped at all.

I have attached the code for the two classes I have to get this done
for
class its making me get behind. If someone out there can please take a
look
at it and help me out. I have tried it all and just cant get it. Once I
see
how it is done it will help me to understand. I really need help bad. I
have
looked for days on how to do it.
Code is pasted below. How do you f'n do this.
Thanks Allen

<<snip>>



Nov 21 '05 #15

P: n/a
Allen,
Again:
Remember that the
property names need to match exactly...
Hint: The property names need to match "Exactly", not "exactly". :-)
I hope you understand that I cannot do your homework for you.


For today's extra credit, I'm letting you figure out what I mean by
"Exactly"! :-))

Hope this helps
Jay

"Allen" <me***@pacbell.net> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl... Thanks for your help I found that I did not have Customer.Initialize()
In
the code. I still have a problem with getting the phone number out of the
array.
what is working.
Customer.Initialize()
customers.Add(New Customer)
LstBoxCustomer.DisplayMember = "CustomerName"
LstBoxCustomer.ValueMember = "phoneNo"
LstBoxCustomer.DataSource = customers

I have a new listbox on another form that needs to show the Phone numbers.
I
thought I could just change the code to show the phone number but It
doesnt
work.

Customer.Initialize()
customers = aCustomer.GetAll()
customers.Add(New Customer)
lstBoxPhone.DisplayMember = "phoneno"
lstBoxPhone.ValueMember = "phoneno"
lstBoxPhone.DataSource = customers

thanks
Allen

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e7**************@TK2MSFTNGP09.phx.gbl...
Allen,
Thanks for the info but it still is not working write.

The sample I gave should work with very minor tweaking. Remember that the
property names need to match exactly...

There is one minor "quirk" in my code. You should set the DisplayMember &
ValueMember before you set the DataSource, otherwise the
ListBox.SelectedIndexChanged event can occur with "different" values for
the
SelectedValue property (as ValueMember has not be set yet). Alternatively
you can check the type of SelectedValue before you use it.

I cant change the
class code it came from my instructor and we have to work with it.

This is Homework? :-(

I hope you understand that I cannot do your homework for you. As you are
less likely to actually learn anything....

Again the sample I gave should work with minor tweaking.

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:e8**************@TK2MSFTNGP14.phx.gbl...
Thanks for the info but it still is not working write. I cant change the
class code it came from my instructor and we have to work with it.
Here is what I am doing on a forms onload event

Dim cCustomer As New Customer
Dim arCust As New ArrayList
Dim key As String
Private Sub Find_Customer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim lst As New ArrayList
lst = cCustomer.GetAll()
LstBoxCustomer.DataSource = lst
LstBoxCustomer.DisplayMember = "Name"
LstBoxCustomer.ValueMember = "phoneNo"
and I get this error : Could not bind to the new display member.
In the debug window I get this.

- lst {Length=5} System.Collections.ArrayList
+ (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (1) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (2) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (3) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (4) {WindowsApplication2.Customer} WindowsApplication2.Customer

when I expand each one I get this.

- lst {Length=5} System.Collections.ArrayList
- (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
address "Atlanta" String
CustomerName "Eleanor" String
name "Eleanor" String
phoneNo "123-4567" String

Any help Jay would be great. It should not be that hard to fill a
listbox
with name and then use a find button to find the phone number and
address
to
be put into textboxes. But we have to use what he gave us. datasets
would
be
easier.

Thanks
Allen
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Allen,
When posting code its "better" to put the code in a Zip file, as .vb
files
are normally blocked to ensure you are not attempting to spread a
virus.
Or
inline like you did here...

Its also "better" to stay in the newsgroup you started, rather then
splatter
your message on a number of VB6 newsgroups. Once you have "exhausted"
this
group, then I would recommend turning to other .NET groups...

However!

Four comments on your Customer class:

1) Rather then use "Sub Initialize", I would recommend using "Shared
Sub
New", as it is called automatically for you. (You don't need to
remember
to
explicitly call it).
2) Rather then use comments, I would recommend using Regions to delimit
blocks of code.
3) Your class should use Properties instead of accessor methods
(GetName,
SetName, GetAddress, SetAddress, GetPhoneNo, and SetPhoneNo). As the
data
binding requires Properties.
4) .NET has a predefined "TellAboutSelf" function, its called ToString,
you
should override ToString instead of defining your own function.

So your class becomes:

Public Class Customer

#Region " Attributes "

Private m_name As String
Private m_address As String
Private m_phoneNo As String

#End Region

#Region " Data Access Shared Methods "

Shared Sub New()
' TODO: rename CustomerDA.Initialize to New!
' allowing us to remove shared Customer.New...
CustomerDA.Initialize()
End Sub

Public Shared Function Find(ByVal PhoneNo As String) As Customer
Return CustomerDA.Find(PhoneNo)
End Function

Public Shared Function GetAll() As ArrayList
Return CustomerDA.GetAll
End Function

Public Shared Sub Terminate()
CustomerDA.Terminate()
End Sub

#End Region

#Region " Data Access Instance Methods "

Public Sub AddNew()
CustomerDA.AddNew(Me)
End Sub

Public Sub Update()
CustomerDA.Update(Me)
End Sub

Public Sub Delete()
CustomerDA.Delete(Me)
End Sub

#End Region

#Region " Properties "

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property

Public Property Address() As String
Get
Return m_address
End Get
Set(ByVal value As String)
m_address = value
End Set
End Property

Public Property PhoneNo() As String
Get
Return m_phoneNo
End Get
Set(ByVal value As String)
m_phoneNo = value
End Set
End Property

#End Region

#Region " Tell About Self "

Public Overrides Function ToString() As String
Dim info As String
info = "Name = " & Name & _
", Address = " & Address & _
", Phone No = " & PhoneNo
Return info
End Function

#End Region

#Region " Constructors "

'default constructor
Public Sub New()
End Sub

'constructor (3 parameters)
Public Sub New(ByVal aName As String, ByVal anAddress As String,
_
ByVal aPhoneNo As String)
m_name = aName
m_address = anAddress
m_phoneNo = aPhoneNo
End Sub

#End Region

End Class

Then as Ken stated when you bind to the ListBox, you would use
something
like:

Dim list As ArrayList
Dim ListBox1 As ListBox

ListBox1.DataSource = list
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "PhoneNo"

To get the selected customer from the ListBox you can use the
ListBox.SelectedItem property:
Dim selectedCustomer As Customer =
DirectCast(ListBox1.SelectedItem, Customer)

To get the selected phone number from the ListBox you can use the
ListBox.SelectedValue property:
Dim phoneNo As String = DirectCast(ListBox1.SelectedValue,
String)

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
>I am really begining to hate programming.
> I have to have this thing done for class and cant seem to get it to do
> what
> its supose to do.
> I have two classes one Customers and one CustomersDA
> I have a form that is supose to display customer names in a listbox
> and
> then
> have 3 buttons (find, Update and Close) I cant seem to get things to
> work
> the right way. Everyone has had good input but its not helped at all.
>
> I have attached the code for the two classes I have to get this done
> for
> class its making me get behind. If someone out there can please take a
> look
> at it and help me out. I have tried it all and just cant get it. Once
> I
> see
> how it is done it will help me to understand. I really need help bad.
> I
> have
> looked for days on how to do it.
> Code is pasted below. How do you f'n do this.
> Thanks Allen
>
<<snip>>



Nov 21 '05 #16

P: n/a
Thanks so much for you help. I finally go the idea I had to add a new
property so I could get it. I had to change the class he gave us by adding
it. I thought I could not change it. Working now. Now I have to do the same
in Java Oh BOY!!!!!!!!

Allen

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
Allen,
Again:
Remember that the
property names need to match exactly...


Hint: The property names need to match "Exactly", not "exactly". :-)
I hope you understand that I cannot do your homework for you.


For today's extra credit, I'm letting you figure out what I mean by
"Exactly"! :-))

Hope this helps
Jay

"Allen" <me***@pacbell.net> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl...
Thanks for your help I found that I did not have Customer.Initialize()
In
the code. I still have a problem with getting the phone number out of the
array.
what is working.
Customer.Initialize()
customers.Add(New Customer)
LstBoxCustomer.DisplayMember = "CustomerName"
LstBoxCustomer.ValueMember = "phoneNo"
LstBoxCustomer.DataSource = customers

I have a new listbox on another form that needs to show the Phone
numbers.
I
thought I could just change the code to show the phone number but It
doesnt
work.

Customer.Initialize()
customers = aCustomer.GetAll()
customers.Add(New Customer)
lstBoxPhone.DisplayMember = "phoneno"
lstBoxPhone.ValueMember = "phoneno"
lstBoxPhone.DataSource = customers

thanks
Allen

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e7**************@TK2MSFTNGP09.phx.gbl...
Allen,
Thanks for the info but it still is not working write.
The sample I gave should work with very minor tweaking. Remember that
the
property names need to match exactly...

There is one minor "quirk" in my code. You should set the DisplayMember
&
ValueMember before you set the DataSource, otherwise the
ListBox.SelectedIndexChanged event can occur with "different" values for
the
SelectedValue property (as ValueMember has not be set yet).
Alternatively
you can check the type of SelectedValue before you use it.
I cant change the
class code it came from my instructor and we have to work with it.
This is Homework? :-(

I hope you understand that I cannot do your homework for you. As you are
less likely to actually learn anything....

Again the sample I gave should work with minor tweaking.

Hope this helps
Jay
"Allen" <me***@pacbell.net> wrote in message
news:e8**************@TK2MSFTNGP14.phx.gbl...
Thanks for the info but it still is not working write. I cant change
the
class code it came from my instructor and we have to work with it.
Here is what I am doing on a forms onload event

Dim cCustomer As New Customer
Dim arCust As New ArrayList
Dim key As String
Private Sub Find_Customer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim lst As New ArrayList
lst = cCustomer.GetAll()
LstBoxCustomer.DataSource = lst
LstBoxCustomer.DisplayMember = "Name"
LstBoxCustomer.ValueMember = "phoneNo"
and I get this error : Could not bind to the new display member.
In the debug window I get this.

- lst {Length=5} System.Collections.ArrayList
+ (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (1) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (2) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (3) {WindowsApplication2.Customer} WindowsApplication2.Customer
+ (4) {WindowsApplication2.Customer} WindowsApplication2.Customer

when I expand each one I get this.

- lst {Length=5} System.Collections.ArrayList
- (0) {WindowsApplication2.Customer} WindowsApplication2.Customer
address "Atlanta" String
CustomerName "Eleanor" String
name "Eleanor" String
phoneNo "123-4567" String

Any help Jay would be great. It should not be that hard to fill a
listbox
with name and then use a find button to find the phone number and
address
to
be put into textboxes. But we have to use what he gave us. datasets
would
be
easier.

Thanks
Allen
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> Allen,
> When posting code its "better" to put the code in a Zip file, as .vb
> files
> are normally blocked to ensure you are not attempting to spread a
> virus.
> Or
> inline like you did here...
>
> Its also "better" to stay in the newsgroup you started, rather then
> splatter
> your message on a number of VB6 newsgroups. Once you have "exhausted"
> this
> group, then I would recommend turning to other .NET groups...
>
> However!
>
> Four comments on your Customer class:
>
> 1) Rather then use "Sub Initialize", I would recommend using "Shared
> Sub
> New", as it is called automatically for you. (You don't need to
> remember
> to
> explicitly call it).
> 2) Rather then use comments, I would recommend using Regions to
> delimit
> blocks of code.
> 3) Your class should use Properties instead of accessor methods
> (GetName,
> SetName, GetAddress, SetAddress, GetPhoneNo, and SetPhoneNo). As the
> data
> binding requires Properties.
> 4) .NET has a predefined "TellAboutSelf" function, its called
> ToString,
> you
> should override ToString instead of defining your own function.
>
> So your class becomes:
>
> Public Class Customer
>
> #Region " Attributes "
>
> Private m_name As String
> Private m_address As String
> Private m_phoneNo As String
>
> #End Region
>
> #Region " Data Access Shared Methods "
>
> Shared Sub New()
> ' TODO: rename CustomerDA.Initialize to New!
> ' allowing us to remove shared Customer.New...
> CustomerDA.Initialize()
> End Sub
>
> Public Shared Function Find(ByVal PhoneNo As String) As
> Customer
> Return CustomerDA.Find(PhoneNo)
> End Function
>
> Public Shared Function GetAll() As ArrayList
> Return CustomerDA.GetAll
> End Function
>
> Public Shared Sub Terminate()
> CustomerDA.Terminate()
> End Sub
>
> #End Region
>
> #Region " Data Access Instance Methods "
>
> Public Sub AddNew()
> CustomerDA.AddNew(Me)
> End Sub
>
> Public Sub Update()
> CustomerDA.Update(Me)
> End Sub
>
> Public Sub Delete()
> CustomerDA.Delete(Me)
> End Sub
>
> #End Region
>
> #Region " Properties "
>
> Public Property Name() As String
> Get
> Return m_name
> End Get
> Set(ByVal value As String)
> m_name = value
> End Set
> End Property
>
> Public Property Address() As String
> Get
> Return m_address
> End Get
> Set(ByVal value As String)
> m_address = value
> End Set
> End Property
>
> Public Property PhoneNo() As String
> Get
> Return m_phoneNo
> End Get
> Set(ByVal value As String)
> m_phoneNo = value
> End Set
> End Property
>
> #End Region
>
> #Region " Tell About Self "
>
> Public Overrides Function ToString() As String
> Dim info As String
> info = "Name = " & Name & _
> ", Address = " & Address & _
> ", Phone No = " & PhoneNo
> Return info
> End Function
>
> #End Region
>
> #Region " Constructors "
>
> 'default constructor
> Public Sub New()
> End Sub
>
> 'constructor (3 parameters)
> Public Sub New(ByVal aName As String, ByVal anAddress As
> String,
> _
> ByVal aPhoneNo As String)
> m_name = aName
> m_address = anAddress
> m_phoneNo = aPhoneNo
> End Sub
>
> #End Region
>
> End Class
>
> Then as Ken stated when you bind to the ListBox, you would use
> something
> like:
>
> Dim list As ArrayList
> Dim ListBox1 As ListBox
>
> ListBox1.DataSource = list
> ListBox1.DisplayMember = "Name"
> ListBox1.ValueMember = "PhoneNo"
>
> To get the selected customer from the ListBox you can use the
> ListBox.SelectedItem property:
> Dim selectedCustomer As Customer =
> DirectCast(ListBox1.SelectedItem, Customer)
>
> To get the selected phone number from the ListBox you can use the
> ListBox.SelectedValue property:
> Dim phoneNo As String = DirectCast(ListBox1.SelectedValue,
> String)
>
> Hope this helps
> Jay
>
>
> "Allen" <me***@pacbell.net> wrote in message
> news:%2****************@TK2MSFTNGP10.phx.gbl...
>>I am really begining to hate programming.
>> I have to have this thing done for class and cant seem to get it to
>> do
>> what
>> its supose to do.
>> I have two classes one Customers and one CustomersDA
>> I have a form that is supose to display customer names in a listbox
>> and
>> then
>> have 3 buttons (find, Update and Close) I cant seem to get things to
>> work
>> the right way. Everyone has had good input but its not helped at all.
>>
>> I have attached the code for the two classes I have to get this done
>> for
>> class its making me get behind. If someone out there can please take
>> a
>> look
>> at it and help me out. I have tried it all and just cant get it. Once
>> I
>> see
>> how it is done it will help me to understand. I really need help bad.
>> I
>> have
>> looked for days on how to do it.
>> Code is pasted below. How do you f'n do this.
>> Thanks Allen
>>
> <<snip>>
>
>



Nov 21 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.