vb.net数组对象列表

vb.net数组对象列表,vb.net,arraylist,Vb.net,Arraylist,我正在尝试创建一个对象的arraylist,除了所有11个对象的值都相同之外,其他一切都正常工作。我尝试过多种编写代码的方法,但每次都得到相同的结果 arrFullProdList收集数据库中所有产品的名称。这是正常工作。这就是我遇到的所有对象都相同的问题 我做错了什么 宣布 Private objReader As SqlDataReader Private objProducts As New CProducts Private arrFullProdList As ArrayList =

我正在尝试创建一个对象的arraylist,除了所有11个对象的值都相同之外,其他一切都正常工作。我尝试过多种编写代码的方法,但每次都得到相同的结果

arrFullProdList收集数据库中所有产品的名称。这是正常工作。这就是我遇到的所有对象都相同的问题

我做错了什么

宣布

Private objReader As SqlDataReader
Private objProducts As New CProducts
Private arrFullProdList As ArrayList = New ArrayList
Public arrProdcuts As ArrayList = New ArrayList
类产品

Public Class CProduct

    Private _pstrProdId As String
    Private _pstrProdDesc As String
    Private _psngWhCost As Single
    Private _psngRetPrice As Single
    Private _pblnTaxable As Boolean
    Private _isNewProd As Boolean

    Public Sub New()

        '_pstrProdId = ""
        '_pstrProdDesc = ""
        '_psngWhCost = 0
        '_psngRetPrice = 0
        '_pblnTaxable = False
        '_isNewProd = False
    End Sub

    Public Property strProdId() As String
        Get
            Return _pstrProdId
        End Get
        Set(strVal As String)
            _pstrProdId = strVal
        End Set
    End Property

    Public Property strProdDesc() As String
        Get
            Return _pstrProdDesc
        End Get
        Set(strVal As String)
            _pstrProdDesc = strVal
        End Set
    End Property

    Public Property sngWhCost() As Single
        Get
            Return _psngWhCost
        End Get
        Set(sngVal As Single)
            _psngWhCost = sngVal
        End Set
    End Property

    Public Property sngRetPrice() As Single
        Get
            Return _psngRetPrice
        End Get
        Set(sngVal As Single)
            _psngRetPrice = sngVal
        End Set
    End Property

    Public Property blnTaxable() As Boolean
        Get
            Return _pblnTaxable
        End Get
        Set(blnVal As Boolean)
            _pblnTaxable = blnVal
        End Set
    End Property

    Public Property IsNewProd() As Boolean
        Get
            Return _isNewProd
        End Get
        Set(blnVal As Boolean)
            _isNewProd = blnVal
        End Set
    End Property

    Public ReadOnly Property GetSaveParameters() As ArrayList
        Get
            Dim paramList As New ArrayList
            paramList.Add(New SqlClient.SqlParameter("ProdId", _pstrProdId))
            paramList.Add(New SqlClient.SqlParameter("ProdDesc", _pstrProdDesc))
            paramList.Add(New SqlClient.SqlParameter("WhCost", _psngWhCost))
            paramList.Add(New SqlClient.SqlParameter("RetPrice", _psngRetPrice))
            paramList.Add(New SqlClient.SqlParameter("Taxable", _pblnTaxable))
            Return paramList
        End Get
    End Property

    Public Function Save() As Integer
        'return -1 if the ID already exists and we can't create a new record
        If _isNewProd Then
            Dim strRes As String = myDB.GetSingleValueFromSP("sp_CheckProdIDExists", _
                                                             New SqlClient.SqlParameter("ProdId", _pstrProdId))
            If Not strRes = 0 Then
                Return -1 'ID NOT unique!!
            End If
        End If
        'if not a new member or it is new and is unique, then do the save (update or insert)
        Return myDB.ExecSP("sp_SaveProduct", GetSaveParameters)
    End Function


End Class
C类产品

Imports System.Data.SqlClient
Public Class CProducts

    'This class represents the Members table and the associated business rules
    Private _Product As CProduct
    'constructor
    Public Sub New()
        'instantiate the CMember object
        _Product = New CProduct
    End Sub

    Public ReadOnly Property CurrentObject() As CProduct
        Get
            Return _Product
        End Get
    End Property


    Public Sub Clear()
        _Product = New CProduct
    End Sub

    Public Sub CreateNewProduct() 'call me when you are clearing the screen to create a new member
        Clear()
        _Product.IsNewProd = True
    End Sub

    Public Function Save() As Integer
        Return _Product.Save
    End Function

    Public Function GetProductList() As SqlDataReader
        Return myDB.GetDataReaderBySP("dbo.sp_GetProductList")
    End Function

    Public Function GetProducIdList() As SqlDataReader
        Return myDB.GetDataReaderBySP("dbo.sp_GetProductIdList")
    End Function

    Public Function GetProductByName(strProdDesc As String) As CProduct
        Dim params As New ArrayList
        Dim param1 As New SqlParameter("proddesc", strProdDesc)
        params.Add(param1)
        FillObject(myDB.GetDataReaderBySP("dbo.sp_GetProductByName", params))
        Return _Product
    End Function


    Public Function GetProductById(strProdId As String) As CProduct
        Dim aParam As New SqlParameter("ProdId", strProdId)
        FillObject(myDB.GetDataReaderBySP("dbo.sp_GetProductByID", aParam))
        Return _Product
    End Function


    Public Function FillObject(sqlDR As SqlDataReader) As CProduct
        Using sqlDR
            If sqlDR.Read Then
                With _Product
                    .strProdId = sqlDR.Item("ProdId") & ""
                    .strProdDesc = sqlDR.Item("ProdDesc") & ""
                    .sngWhCost = sqlDR.Item("WhCost") & ""
                    .sngRetPrice = sqlDR.Item("RetPrice") & ""
                    .blnTaxable = sqlDR.Item("Taxable") & ""
                End With
            Else
                'failed for some reason
            End If
        End Using
        Return _Product
    End Function

    '----------Start Alex's Code---------
    Public Function GetProductByDesc(strProdDesc As String) As SqlDataReader
        Dim aParam As New SqlParameter("ProdDesc", strProdDesc)
        Return myDB.GetDataReaderBySP("dbo.sp_GetProductByDesc", aParam)
    End Function
End Class
主要


什么是
objReader
objProducts
GetProductByDesc
CProduct
?请提供一个。我对“arrProdcuts.Add…”一行感到困惑。这是什么数据类型?我在代码中没有看到Add方法。它看起来像是CProducts,因为很清楚,但我没有看到添加。你的问题的核心是,你在列表中一次又一次地添加对同一对象的引用,但我们需要更多的代码来查看到底发生了什么。Jeremy,谢谢你的评论。您还需要查看哪些其他代码?arrProducts是arraylist,arrFullProdList也是。arrFullProdList只收集每个产品的名称。然后我使用一个循环遍历数据库,收集一个CProduct类型的对象,并将每个对象添加到arrcroducts中。如果您遵循_Product变量的路径,它在数据填充之间永远不会被设置为“新”实例,因此您所做的是将同一实例反复添加到列表中,因此,列表中的每个元素都指向内存中的相同位置。
Private Sub LoadProducts()
    arrProdcuts.Clear()
    objReader = objProducts.GetProductByDesc(txtSearch.Text)
    While objReader.Read
        arrFullProdList.Add(objReader.Item("prodDesc"))
    End While
    objReader.Close()

    For i = 0 To arrFullProdList.Count - 1
        Dim aNewProd As CProduct
        aNewProd = objProducts.GetProductByName(arrFullProdList.Item(i).ToString)
        arrProdcuts.Add(aNewProd)
    Next
End Sub