簡體   English   中英

如何檢查arraylist是否已經包含對象

[英]How to check if an arraylist already contains an object

我正在嘗試讓我的購物車識別是否已經將商品添加到購物車,然后將數量更新為另一個,但是它只會使該語句為false。

這是我的頁面加載處理程序,它根據查詢字符串中發送的詳細信息創建新產品。

Private Sub ShoppingCartPage_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not (IsPostBack) Then
        If Not (Request.QueryString.ToString().Length.Equals(0)) Then
            Dim newProduct As Product = New Product(Request.QueryString("ProductCode"), Request.QueryString("ProductName"), Request.QueryString("Category"), Val(Request.QueryString("Price")), Request.QueryString("Description"))

            If Session("shoppingCartSession") Is Nothing Then
                shoppingCart = New ArrayList()
                shoppingCart.Add(newProduct)
                Session("shoppingCartSession") = shoppingCart
            ElseIf (shoppingCart.Contains(newProduct.itemID)) Then
                For Each item As Product In shoppingCart
                    If item.Equals(Request.QueryString("ProductCode")) Then
                        item.updateQuantity()
                    End If
                Next

            Else
                shoppingCart = CType(Session("shoppingCartSession"), ArrayList)
                shoppingCart.Add(newProduct)
                Session.Add("shoppingCartSession", shoppingCart)
            End If
        End If
        shoppingCart = CType(Session("shoppingCartSession"), ArrayList)
        createShoppingCartTable()
    End If

End Sub

這里有很多問題。 首先,如果要像這樣添加到ArrayList

shoppingCart.Add(newProduct)

那么您要添加Product對象。 在這種情況下,您為什么期望此功能有用:

If (shoppingCart.Contains(newProduct.itemID)) Then

newProduct.itemID大概是Integer之類的,因此當然集合中不包含它。 它不包含任何Integer值,因為它包含Product對象。 您需要檢查它是否包含具有該itemIDProduct對象,而不是它是否直接包含該itemID 我將替換所有這些:

If Session("shoppingCartSession") Is Nothing Then
    shoppingCart = New ArrayList()
    shoppingCart.Add(newProduct)
    Session("shoppingCartSession") = shoppingCart
ElseIf (shoppingCart.Contains(newProduct.itemID)) Then
    For Each item As Product In shoppingCart
        If item.Equals(Request.QueryString("ProductCode")) Then
            item.updateQuantity()
        End If
    Next

Else
    shoppingCart = CType(Session("shoppingCartSession"), ArrayList)
    shoppingCart.Add(newProduct)
    Session.Add("shoppingCartSession", shoppingCart)
End If

有了這個:

shoppingCart = TryCast(Session("shoppingCartSession"), ArrayList)

If shoppingCart Is Nothing Then
    shoppingCart = New ArrayList
    Session("shoppingCartSession") = shoppingCart
End If

Dim existingProduct = shoppingCart.Cast(Of Product)().
                                   SingleOrDefault(Function(p) p.itemID = newProduct.itemID)

If existingProduct Is Nothing Then
    shoppingCart.Add(newProduct)
Else
    existingProduct.updateQualtity()
End If

也就是說,我還將遵循@VisualVincent的建議,並使用List(Of Product)而不是ArrayList 如果這樣做,則可以在我建議的代碼中省略Cast調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM