2012-08-30 143 views
0

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

一种是像“%中生集团%”

另一种是像“%CN-%”

这两款产品代码,而不是被添加到同一个集合理性单项交易。我只想显示CN-产品不能与CNBG产品一起输入的错误。

这是我的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 
+0

其他注意事项:检查将在s.ItemCD我想确保在这个购物车,他们不能添加CNBG产品,例如;如果输入了CN-BIC001,则无法输入CNBG-BIC007项目代码。 – user1636949

+0

您使用的是什么版本的框架?更具体地说,你可以使用LINQ吗?该解决方案是否需要易于编码或快速执行(或两者兼而有之)?你能告诉我们如何定义ItemCollection,因为它基于什么类型的集合? – eol

+0

Framework 2.0,但可以达到3.5。我还用购物车项目集合的类编辑了我的上述项目。 – user1636949

回答

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,而不是包含如果代码总是在开头。