views:

22

answers:

1

Hi.

I am not sure how to phrase my question properly but I want to achieve something like this.

I have a class named 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

In one of my code behind pages, I am retrieving data from an SQL Command and placing the same into a datareader object.

How would I be able to declare the class so that each row in my datareader would actually be an instance of the said class?

Like for example:

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

When I do the same, I am getting an error "Object Reference Not Set to an Instance of an Object.

I am quite stumped on this one. Any tips greatly appreciated. Thanks.

A: 

You should use an Arraylist or -better- a generic List(of Product). Besides i would strongly recommend to set Option Strict On in your project's Compiler Settings.

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
Tim Schmelter