vb.net对象的arraylist

时间:2015-04-26 05:53:05

标签: vb.net arraylist

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

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

我做错了什么?

声明

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

类CProduct

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

类CProducts

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

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

0 个答案:

没有答案