开发者

ASP.NET Remove all items from ShopCart

Hey I have ShoppCart class which contains items of Class cartItems

So I can run a functions like

ShoppingCart.Instance.AddItem(5, 5, 16.0, "First Product", 15, 10)

ShoppingCar开发者_如何学运维t.Instance.Items.Count

ShoppingCart.Instance.RemoveItem(productId)

Where the remove functions looks like

' RemoveItem() - Removes an item from the shopping cart
Public Sub RemoveItem(ByVal productId As Integer)
    Dim removedItem = New CartItem(productId)

    For Each item As CartItem In Items
        If item.Equals(removedItem) Then
            Items.Remove(item)
            Return
        End If
    Next
End Sub

That removes one item, I am looking at a function which I can use to EMPTY my shopping cart i.e. on logout I tried Session.Abandon, but then I login again it still remember the items in my cart.


try calling ShoppingCart.Instance.Items.Clear();

and just a random guess, if you are using sample cart code given here: http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=423 (because it seems similar) then what you can also do is, in ShoppingCart class add a new method that clear all items like this:

public void RemoveAll() {  
Items.Clear();
}


It's unclear what sort of list is being used in you shopping cart but you could try -

Items.RemoveAll()

EDIT

I think the 'RemoveAll()' method requires a predicate as an argument, which would mean you'd need to do something like this (which would work but is a weird thing to do) -

Items.RemoveAll(x => 1==1)

Or in vb

list.RemoveAll(Function(x) 1 = 1) 

You could try this -

Items.RemoveRange(0, Items.Count)


You could simply do this

ShoppingCart.Instance.Items.RemoveAll()

EDIT

Not sure which collection is being used. I guess another simple way would be to reinitialize the collection

ShoppingCart.Instance.Items = new Items();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜