HELP!! class that returns an arraylist

A

Allen

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
 
G

Guest

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
 
A

Allen

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
 
C

Cor Ligthert

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
 
K

Ken Tucker [MVP]

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

Allen

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
A

Allen

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
 
J

Jeff Johnson [MVP: VB]

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

Michael Cole

Allen said:
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.
 
A

Allen

sorry about that.
I didnt notice that the others were not .net newsgroups.
Any ideas on my problem?

Allen
 
J

Jay B. Harlow [MVP - Outlook]

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 said:
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>>
 
A

Allen

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
A

Allen

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
 
J

Jay B. Harlow [MVP - Outlook]

Allen,
Again:
Remember that the
property names need to match exactly...

Hint: The property names need to match "Exactly", not "exactly". :)

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

Hope this helps
Jay
 
A

Allen

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
 

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