救命!!返回arraylist的类 [英] HELP!! class that returns an arraylist
问题描述
我有一个返回arraylist的类。如何填写
返回的列表框?它返回的是一个arraylist的客户,但我似乎无法获得填写列表框的东西
。我刚学习并且真的需要一些帮助
坏。
公共共享函数GetAll()如ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String =" SELECT Name,Address,PhoneNo" &安培; _
" FROM CustomerTable"
尝试
Dim adpCustomer As New _
OleDbDataAdapter( sqlQuery,cnnCustomer)
adpCustomer.Fill(dsCustomer," CustTable")
如果dsCustomer.Tables(" CustTable")。Rows.Count> 0然后
Dim dsRow As DataRow
''清除数组列表
customers.Clear()
对于dsCustomer.Tables中的每个dsRow(CustTable)。行
name = dsRow(" Name")
address = dsRow (地址)
phoneno = dsRow(" PhoneNo")
Dim aCustomer As New _
客户(姓名,地址, phoneno)
customers.Add(aCustomer)
下一页
Else
''没有记录在数据库中
结束如果
dsCustomer = Nothing
捕获e作为例外
Console.WriteLine (e.ToString)
抛出新的NotFoundException(未找到)
结束尝试
返回客户>
结束功能
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
推荐答案
我可能错过了它,但我找不到你把客户定在哪里
数组列表如:
将客户视为arr aylist = new arraylist
" Allen"写道:
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:
我有一个返回arraylist的类。如何填写
返回的列表框?它返回的客户是一个arraylist但我似乎无法获得填充列表框的东西。我只是学习并且真的需要一些帮助
不好。
公共共享函数GetAll()As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String =" SELECT Name ,地址,电话号码 &安培; _
" FROM CustomerTable"
尝试
将adpCustomer视为新_
OleDbDataAdapter(sqlQuery,cnnCustomer)
adpCustomer.Fill(dsCustomer," CustTable")
如果dsCustomer.Tables(" CustTable")。Rows.Count> 0然后
Dim dsRow As DataRow
''清除数组列表
customers.Clear()
对于dsCustomer.Tables中的每个dsRow(" ; CustTable")。行
name = dsRow(" Name")
address = dsRow(" Address")
phoneno = dsRow(" PhoneNo")
Dim aCustomer As New _
顾客(姓名,地址,phoneno)
customers.Add(aCustomer)
下一页
其他
''数据库中没有记录
结束如果
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
抛出新的NotFoundException(Not Found" )
结束尝试
返回客户
结束功能
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
Dennis,谢谢帮我。我正在从表单中调用该函数。
这是我在表单上的onLoad事件中所拥有的。
Dim Cust As Customer
Dim carr As ArrayList = New ArrayList
carr = Cust.GetAll
LstBoxCustomer.DataSource = carr
Dim x As整数
x = carr.Count
Dim i As Integer
虽然我< carr.Count
LstBoxCustomer.Items.Add(carr.Item(i).ToString())
i + = 1
结束时< br $> b $ b End Sub
我一直在尝试各种各样的东西,但似乎没什么用。那段时间
的声明是我最后一次尝试,所以我把所有内容都留在那里给你看
我一直在尝试的东西。
Allen
" Dennis" <德**** @ discussions.microsoft.com>在留言中写道
新闻:BC ********************************** @ microsof t.com ...
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...
我可能已经错过了它,但我无法找到你在哪里确定客户的数量
如下数组列表:
昏暗的客户as arraylist = new arraylist
Allen写道:
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:
我有一个返回arraylist的类。如何填写
返回的列表框?它返回的客户是一个arraylist但我似乎无法获得填充列表框的东西。我只是学习并且真的需要一些
帮助
不好。
公共共享函数GetAll()As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = SELECT Name,Address,PhoneNo &安培; _
" FROM CustomerTable"
尝试
将adpCustomer视为新_
OleDbDataAdapter(sqlQuery,cnnCustomer)
adpCustomer.Fill(dsCustomer," CustTable")
如果dsCustomer.Tables(" CustTable")。Rows.Count> 0然后
Dim dsRow As DataRow
''清除数组列表
customers.Clear()
对于dsCustomer.Tables中的每个dsRow(" ; CustTable")。行
name = dsRow(" Name")
address = dsRow(" Address")
phoneno = dsRow(" PhoneNo")
Dim aCustomer As New _
顾客(姓名,地址,phoneno)
customers.Add(aCustomer)
下一页
其他
''数据库中没有记录
结束如果
dsCustomer = Nothing
Catch e As Exception
Console.WriteLine(e.ToString)
抛出新的NotFoundException(Not Found" )
结束尝试
返回客户
结束功能
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
艾伦,
你是否有一位教练告诉你应该使用这个
疯狂的建筑。
我假设你是,所以我只回答你的技术错误。
在列表框中你可以使用数据源或填充项目,在一个控件中有两种可能性,但不能合并。所以至少你必须要做的是*不是*设置数据源。关于你的代码,请使用
语句更容易(我在1.0中显示1.1的代码,这是一个
有点不同说明了) 。
\\\
我的整数= 0到carr.Count -1
LstBoxCustomer.Items .Add(carr.Item(i).ToString())
下一页
///
我希望这会有所帮助?
Cor
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
这篇关于救命!!返回arraylist的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!