hi,
i have the following code which adds the items to the shoppingcart by session
but i need to use cookies instead!! when i use cookies i get error converting cookie to shoppingcat!!
Imports Microsoft.VisualBasic
Public Class ShoppingCart
#Region "Properties"
Private _items As List(Of CartItem)
Public Property Items() As List(Of CartItem)
Get
Return _items
End Get
Private Set(ByVal value As List(Of CartItem))
_items = value
End Set
End Property
#End Region
#Region "Singleton Implementation"
Public Shared ReadOnly Instance As ShoppingCart
Shared Sub New()
If HttpContext.Current.Session("ASPNETShoppingCart") Is Nothing Then
Instance = New ShoppingCart()
Instance.Items = New List(Of CartItem)
HttpContext.Current.Session("ASPNETShoppingCart") = Instance
Else
Instance = CType(HttpContext.Current.Session("ASPNETShoppingCart"), ShoppingCart)
End If
End Sub
Protected Sub New()
End Sub
#End Region
#Region "Item Modification Methods"
Public Sub AddItem(ByVal productId As Integer)
Dim newItem = New CartItem(productId)
If Items.Contains(newItem) Then
For Each item As CartItem In Items
If item.Equals(newItem) Then
item.Quantity += 1
Return
End If
Next
Else
newItem.Quantity = 1
Items.Add(newItem)
End If
End Sub
Public Sub SetItemQuantity(ByVal productId As Integer, ByVal quantity As Integer)
If quantity = 0 Then
RemoveItem(productId)
Return
End If
Dim updatedItem = New CartItem(productId)
For Each item As CartItem In Items
If item.Equals(updatedItem) Then
item.Quantity = quantity
Return
End If
Next
End Sub
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
Public Sub RemoveAllItems()
For Each item As CartItem In Items
' Items.Remove(item.ProductId)
Next
End Sub
#End Region
#Region "Reporting Methods"
Public Function GetSubTotal() As Decimal
Dim subTotal As Decimal = 0
For Each item As CartItem In Items
subTotal += item.TotalPrice
Next
Return subTotal
End Function
Public Function GetTotalItemsInCart() As String
Dim Total As Decimal = 0
For Each item As CartItem In Items
Total += item.Quantity
Next
Return Total
End Function
#End Region
End Class