查找项集合中的现有项目

时间:2012-08-30 19:26:23

标签: vb.net itemcollection

下面是我的代码。我试图在项目之后或项目添加之前找到特定项目。我们有两个特殊产品代码,不能在同一个产品集合中。

一个是"%CNBG%"

另一个是喜欢"%CN - %"

由于单独交易的原因,这两种产品代码并未添加到同一集合中。我只是想显示一个错误,即无法用CNBG产品输入CN-产品。

这是我的AddStormItem,它将它广告到该集合,我认为我的检查需要在这里。如何找到我在下面添加的集合中的现有项目?这样我就可以对它进行检查。

Private Sub AddStoreItem(ByVal ProductCd As String, ByVal ddlSearchField As String, ByVal tbSearchValue As String)

    Dim cartitems As Cart = OnsiteCart

    Dim s As New dB.Product
    Dim dt As DataTable = Store.GetProductbyCd(ProductCd)
    Dim ds As New DataSet
    Dim er As New Visitor

    ds = Common.GetCustomerByParam(OnsiteCart.CustomerCD, "", "", "", "", "")

    er.IsMemberFlg = nullCStr(ds.Tables(0).Rows(0)("IsMemberFlg"))

    VisitorData = er

    Try

        If VisitorData.IsMemberFlg <> "N" Then

            s.Amount = Format(dt.Rows(0)("UDEF2AMT"), "0.00")
            s.Description = dt.Rows(0)("PRODUCTNAME")
            s.ItemCD = dt.Rows(0)("PRODUCTCD")
            s.Quantity = 1

        Else
            s.Amount = Format(dt.Rows(0)("UNITPRICE"), "0.00")
            s.Description = dt.Rows(0)("PRODUCTNAME")
            s.ItemCD = dt.Rows(0)("PRODUCTCD")
            s.Quantity = 1

        End If

    Catch ex As Exception
        lblProductError.Text = "Product not found"
        lblProductError.Visible = True
    End Try

    If s.Amount <> 0 Then
        cartitems.ItemCollection.Add(s)
        lblProductError.Visible = False
    End If

End Sub

这是我的购物车类

Imports system
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI
Imports System.Collections.ObjectModel

<Serializable()> _
Public Class Cart

#Region " Private Variables "

    Private strenterusercd As String
    Private strinvoicenum As String
    Private strinvoicenumreused As Boolean
    Private strpayeecd As String
    Private strpayeetypecd As String
    Private strcustomercd As String

    Private strpaymenttype As enPaymentType
    Private dblcashamount As Double
    Private dblcheckamount As Double
    Private strchecknumber As String
    Private strponumber As String

    Private strCCType As String
    Private strCCNumber As String
    Private strCCNumberEncrypted As String
    Private strCCExp As String
    Private strCCZip As String
    Private strCCCVV As String

    Private strCCVerificationcd As String
    Private strCCResponsecd As String
    Private strCCRejectioncd As String
    Private strCCTransactioncd As String

    Private intmaxID As Int32

    Private dblCCAmount As Double

#End Region

    Public ItemCollection As New CartItems(Of iInvoiceItem)

    Enum enPaymentType
        CreditCard
        Cash
        Check
        PO
    End Enum

#Region " Public Properties "

    Public Property EnterUserCd() As String
        Get
            Return strenterusercd
        End Get
        Set(ByVal value As String)
            strenterusercd = value
        End Set
    End Property

    Public Property InvoiceNumReUsed() As Boolean
        Get
            Return strinvoicenumreused
        End Get
        Set(ByVal value As Boolean)
            strinvoicenumreused = value
        End Set
    End Property

    Public Property InvoiceNum() As String
        Get
            Return strinvoicenum
        End Get
        Set(ByVal value As String)
            strinvoicenum = value
        End Set
    End Property

    Public Property PayeeCd() As String
        Get
            Return strpayeecd
        End Get
        Set(ByVal value As String)
            strpayeecd = value
        End Set
    End Property

    Public Property PayeeTypeCd() As String
        Get
            Return strpayeetypecd
        End Get
        Set(ByVal value As String)
            strpayeetypecd = value
        End Set
    End Property

    Public Property CustomerCD() As String
        Get
            Return strcustomercd
        End Get
        Set(ByVal value As String)
            strcustomercd = value
        End Set
    End Property

    Public Property PaymentType() As enPaymentType
        Get
            Return strpaymenttype
        End Get
        Set(ByVal value As enPaymentType)
            strpaymenttype = value
        End Set
    End Property

    Public Property CashAmount() As Double
        Get
            Return dblcashamount
        End Get
        Set(ByVal value As Double)
            dblcashamount = value
        End Set
    End Property

    Public Property CheckAmount() As Double
        Get
            Return dblcheckamount
        End Get
        Set(ByVal value As Double)
            dblcheckamount = value
        End Set
    End Property

    Public Property CheckNumber() As String
        Get
            Return strchecknumber
        End Get
        Set(ByVal value As String)
            strchecknumber = value
        End Set
    End Property

    Public Property PONumber() As String
        Get
            Return strponumber
        End Get
        Set(ByVal value As String)
            strponumber = value
        End Set
    End Property

    Public Property CCType() As String
        Get
            Return strCCType
        End Get
        Set(ByVal value As String)
            strCCType = value
        End Set
    End Property

    Public Property CCNumber() As String
        Get
            Return strCCNumber
        End Get
        Set(ByVal value As String)
            strCCNumber = value
        End Set
    End Property

    Public ReadOnly Property CCNumberEncrypted() As String
        Get
            If CCNumber Is Nothing Then
                Return ""
            ElseIf CCNumber.Length = 16 Then
                Return "************" & Right(CCNumber, 4)
            Else
                Return "***********" & Right(CCNumber, 4)
            End If
        End Get
    End Property

    Public Property CCExp() As String
        Get
            Return strCCExp
        End Get
        Set(ByVal value As String)
            strCCExp = value
        End Set
    End Property

    Public Property CCZip() As String
        Get
            Return strCCZip
        End Get
        Set(ByVal value As String)
            strCCZip = value
        End Set
    End Property

    Public Property CCCVV() As String
        Get
            Return strCCCVV
        End Get
        Set(ByVal value As String)
            strCCCVV = value
        End Set
    End Property

    Public Property CCAmount() As Double
        Get
            Return dblCCAmount
        End Get
        Set(ByVal value As Double)
            dblCCAmount = value
        End Set
    End Property

    Public Property CCVerificationcd() As String
        Get
            Return strCCVerificationcd
        End Get
        Set(ByVal value As String)
            strCCVerificationcd = value
        End Set
    End Property

    Public Property CCResponsecd() As String
        Get
            Return strCCResponsecd
        End Get
        Set(ByVal value As String)
            strCCResponsecd = value
        End Set
    End Property

    Public Property CCRejectioncd() As String
        Get
            Return strCCRejectioncd
        End Get
        Set(ByVal value As String)
            strCCRejectioncd = value
        End Set
    End Property

    Public Property CCTransactioncd() As String
        Get
            Return strCCTransactioncd
        End Get
        Set(ByVal value As String)
            strCCTransactioncd = value
        End Set
    End Property

    Public Property MaxID() As Int32
        Get
            Return intmaxID
        End Get
        Set(ByVal value As Int32)
            intmaxID = value
        End Set
    End Property

#End Region

    <Serializable()> _
    Public Class CartItems(Of itemType As iInvoiceItem)
        Inherits Collection(Of iInvoiceItem)
    End Class

    Public Sub New()
        Me.EnterUserCd = HttpContext.Current.User.Identity.Name
        Me.InvoiceNumReUsed = False
        Me.InvoiceNum = Nothing 'Common.getAutonumber("INVOICE")
        Me.MaxID = 0
    End Sub

    Public Sub New(ByVal invoicenum As String)
        Me.EnterUserCd = HttpContext.Current.User.Identity.Name
        Me.InvoiceNumReUsed = True
        Me.InvoiceNum = invoicenum
        Me.MaxID = 0
    End Sub

    Public Sub New(ByVal payeecd As String, ByVal payeetypecd As String)
        Me.EnterUserCd = HttpContext.Current.User.Identity.Name
        Me.InvoiceNum = Nothing 'Common.getAutonumber("INVOICE")
        Me.InvoiceNumReUsed = False
        Me.PayeeCd = payeecd
        Me.PayeeTypeCd = payeetypecd
        Me.MaxID = 0
    End Sub

    Public Sub New(ByVal invoicenum As String, ByVal payeecd As String, ByVal payeetypecd As String)
        Me.EnterUserCd = HttpContext.Current.User.Identity.Name
        Me.InvoiceNum = invoicenum
        Me.InvoiceNumReUsed = True
        Me.PayeeCd = payeecd
        Me.PayeeTypeCd = payeetypecd
        Me.MaxID = 0
    End Sub

    Public Function CartTotal()
        Dim total As Double = 0

        For Each item As iInvoiceItem In Me.ItemCollection
            total += item.getPrice
            'total += item.Amount * item.Quantity
        Next

        Return total
    End Function

    Public Function getNewCartItemID() As Int32
        Dim ID As Int32 = 0

        Me.MaxID += 1
        ID = Me.MaxID

        Return ID
    End Function

    Public Function getParentIndSession(ByVal assignedsessioncd As String) As IndSession
        Dim S As New IndSession

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type = "Session" Then
                If CType(item, dB.IndSession).Assignedsessioncd = assignedsessioncd Then
                    S = CType(item, dB.IndSession)
                End If
            End If
        Next
        Return S
    End Function

    Public Function getChildIndfunctions(ByVal assignedsessioncd As String) As CartItems(Of iInvoiceItem)
        Dim tempcart As New CartItems(Of iInvoiceItem)

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type = "Function" Then
                If CType(item, dB.IndFunction).Assignedsessioncd = assignedsessioncd Then
                    tempcart.Add(CType(item, dB.IndFunction))
                End If
            End If
        Next
        Return tempcart
    End Function

    Public Sub DeleteByAssignedsessioncd(ByVal assignedsessioncd As String)
        Dim tempcart As New CartItems(Of iInvoiceItem)

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type = "Session" Then
                If CType(item, dB.IndSession).Assignedsessioncd = assignedsessioncd Then
                    tempcart.Add(item)
                End If
            ElseIf item.Type = "Function" Then
                If CType(item, dB.IndFunction).Assignedsessioncd = assignedsessioncd Then
                    tempcart.Add(item)
                End If
            End If
        Next

        For Each deleteditem As iInvoiceItem In tempcart
            Me.ItemCollection.Remove(deleteditem)
        Next
    End Sub

    Public Function getCompCodes() As ArrayList
        Dim arrCompCodes As New ArrayList

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type = "Function" Then
                If Not CType(item, dB.IndFunction).Compcd Is Nothing Then
                    arrCompCodes.Add(CType(item, dB.IndFunction).Compcd)
                End If
            End If
        Next
        Return arrCompCodes
    End Function

    Public Function getUsedFreeFullRegistrations() As Int32
        Dim count As Int32 = 0

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type = "Session" Then
                If CType(item, dB.IndSession).ItemCD = "EXHIB_XC" Then
                    count += 1
                End If
            End If
        Next
        Return count
    End Function

    Public Function getUsedFreeExhibitOnlyRegistrations() As Int32
        Dim count As Int32 = 0

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type = "Session" Then
                If CType(item, dB.IndSession).ItemCD = "EXHIB_XD" Then
                    count += 1
                End If
            End If
        Next
        Return count
    End Function

    Public Function getUsedFreeExhibitDailyRegistrations() As Int32
        Dim count As Int32 = 0

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type = "Session" Then
                If CType(item, dB.IndSession).ItemCD = "EXHIB_XE" Then
                    count += 1
                End If
            End If
        Next
        Return count
    End Function

    Public Sub Checkout()
        If Me.CartTotal > 0 Then
            System.Web.HttpContext.Current.Response.Redirect("payment.aspx")
        Else
            System.Web.HttpContext.Current.Response.Redirect("confirm.aspx")
        End If
    End Sub

    Public Sub Clear()
        Me.CashAmount = Nothing
        Me.CCAmount = Nothing
        Me.CCCVV = Nothing
        Me.CCExp = Nothing
        Me.CCNumber = Nothing
        Me.CCRejectioncd = Nothing
        Me.CCResponsecd = Nothing
        Me.CCType = Nothing
        Me.CCVerificationcd = Nothing
        Me.CCZip = Nothing
        Me.CheckAmount = Nothing
        Me.CheckNumber = Nothing
        Me.CustomerCD = Nothing
        Me.EnterUserCd = Nothing
        Me.InvoiceNum = Nothing 'Common.getAutonumber("INVOICE")
        Me.InvoiceNumReUsed = Nothing
        Me.ItemCollection = New CartItems(Of iInvoiceItem)
        Me.MaxID = Nothing
        Me.PayeeCd = Nothing
        Me.PayeeTypeCd = Nothing
        Me.PaymentType = Nothing
        Me.PONumber = Nothing
    End Sub

    Public Sub ClearItems()
        Me.CashAmount = Nothing
        Me.CCAmount = Nothing
        Me.CCCVV = Nothing
        Me.CCExp = Nothing
        Me.CCNumber = Nothing
        Me.CCRejectioncd = Nothing
        Me.CCResponsecd = Nothing
        Me.CCType = Nothing
        Me.CCVerificationcd = Nothing
        Me.CCZip = Nothing
        Me.CheckAmount = Nothing
        Me.CheckNumber = Nothing
        Me.InvoiceNumReUsed = Nothing
        Me.ItemCollection = New CartItems(Of iInvoiceItem)
        Me.MaxID = Nothing
        Me.PaymentType = Nothing
        Me.PONumber = Nothing
    End Sub

    Public Function getCartContents() As ArrayList
        Dim arrCartItems As New ArrayList

        For Each item As iInvoiceItem In Me.ItemCollection
            arrCartItems.Add(item)
        Next
        Return arrCartItems
    End Function

    Public Function getSingleUserCartAssignedsessioncd() As String
        Dim retval As String = ""

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type = "Session" Then
                retval = CType(item, dB.IndSession).Assignedsessioncd
            End If
        Next
        Return retval
    End Function

    Public Function getParentItems() As DataTable
        Dim dt As New DataTable

        For Each item As iInvoiceItem In Me.ItemCollection
            If item.Type <> "Function" Then
                If Not CType(item, dB.IndFunction).Compcd Is Nothing Then
                    dt.Rows.Add(CType(item, dB.IndFunction).Compcd)
                End If
            End If
        Next
        Return dt
    End Function

End Class

1 个答案:

答案 0 :(得分:0)

在Framework 2.0中,我认为你必须手动循环。在3.5中,你可以使用Linq ...仍然可以循环,但很容易:

If s.ItemCd.Contains("CNBG") Then
    If (From i In cartitems.ItemCollection Where i.ItemCd.Contains("CN-") Select i).Any() Then
        'Error message         
        Return
    End If
ElseIf s.ItemCd.Contains("CN-") Then
    If (From i In cartitems.ItemCollection Where i.ItemCd.Contains("CNBG") Select i).Any() Then
        'Error message                
        Return
    End If
End If

'Ok if we got here
cartitems.ItemCollection.Add(s)

如果代码始终位于开头,您也可以使用StartsWith而不是Contains。