0

我不确定如何正确表达我的问题,但我想实现这样的目标。

我有一个名为 Products 的课程

public class Products

private ID as Integer

private Name as String

Public Property ProductID()
  Get
    Return ID
  End Get
  Set(ByVal value)
    ID = value
  End Set
End Property

在我的页后代码中,我从 SQL 命令中检索数据并将其放入数据读取器对象中。

我如何能够声明该类,以便我的 datareader 中的每一行实际上都是所述类的实例?

例如:

Dim myProduct() as New Product
Dim intCnt as Integer 

While datareaderData.read()
  intCnt += 1
  myProduct(intCnt) = new Product
  myProduct(intCnt).ID = datareaderData("ID")
  myProduct(intCnt).Name = datareaderData("Name")
End While

当我这样做时,我收到一个错误“对象引用未设置为对象的实例。

我对这个很困惑。非常感谢任何提示。谢谢。

4

1 回答 1

1

您应该使用 Arraylist 或 - 更好 -通用 List(of Product)。此外,我强烈建议您在项目的编译器设置中设置Option Strict On

Dim products As New List(Of Product)
While datareaderData.read()
    Dim nextProduct As New Product
    nextProduct.ProductID = CType(datareaderData("ID"), System.Int32)
    nextProduct.Name = datareaderData("Name").ToString
    products.add(nextProduct)
End While
于 2010-06-30T20:07:02.903 回答