我有一个添加OrderItem的购物车。在OrderItem中,我有一个(产品和服务作为类)
我的问题:当我添加产品和服务时,我正在努力检索ListCollection中的项目。
我正在建立一个包含产品和服务目录的网站。我的目标是实现一个存储订单商品的购物车。
我有以下课程:
OrderItem
将是Product
和Service
类的父类Product
:待售产品Service
:待售服务CartItem
:此课程将区分产品和服务ShoppingCart
:将添加OrderItem 我的代码如下。
Public MustInherit Class OrderItem
Private m_enuItemType As TypeOfItem = TypeOfItem.None
Private m_strUserID As String = ""
Public Sub New(ByVal enuItemType As TypeOfItem)
m_enuItemType = enuItemType
End Sub
Public Enum TypeOfItem
None = 0
Product = 1
Service = 2
End Enum
Public Property ItemType() As TypeOfItem
Get
Return m_enuItemType
End Get
Set(ByVal value As TypeOfItem)
m_enuItemType = value
End Set
End Property
Public Property UserID() As String
Get
Return m_strUserID
End Get
Set(ByVal value As String)
m_strUserID = value
End Set
End Property
End Class
Public Class Service
Inherits OrderItem
'service Booking Business object
Private objServiceBooking As Bo_Dall_Layer.ServiceBooking
Public Sub New(ByVal enuItemType As TypeOfItem)
MyBase.New(enuItemType)
End Sub
Public Property CartService() As Bo_Dall_Layer.ServiceBooking
Get
Return objServiceBooking
End Get
Set(ByVal value As Bo_Dall_Layer.ServiceBooking)
objServiceBooking = value
End Set
End Property
End Class
Public Class Product
Inherits OrderItem
'product Business object
Private objProduct As Bo_Dall_Layer.BoProduct
Public Sub New(ByVal enuItemType As TypeOfItem)
MyBase.New(enuItemType)
End Sub
Public Property Boprd() As Bo_Dall_Layer.BoProduct
Get
Return objProduct
End Get
Set(ByVal value As Bo_Dall_Layer.BoProduct)
objProduct = value
End Set
End Property
End Class
Public Class CartItem
Private m_enuItemType As ItemType
Private m_objOrderItem As OrderItem
Private Enum ItemType
Product = 1
Service = 2
End Enum
Public Property Item(ByVal objItem As OrderItem) As OrderItem
Get
Return m_objOrderItem
End Get
Set(ByVal value As OrderItem)
m_objOrderItem = value
End Set
End Property
End Class
Public Class UserShoppingCart
Private m_objArrListOfItems As List(Of OrderItem)
Public Sub New()
m_objArrListOfItems = New List(Of OrderItem)(10)
End Sub
Public Function AddItem(ByVal objOrderItem As OrderItem) As Boolean
Try
' Validate item to check if it is already in the list
' If yes, increment counter, otherwise add new item to the list
m_objArrListOfItems.Add(objOrderItem)
Catch ex As Exception
' Log error
End Try
End Function
Public ReadOnly Property ListOfItems() As List(Of OrderItem)
Get
Return m_objArrListOfItems
End Get
End Property
End Class
//检索orderitem以进行显示
Dim arr As New ArrayList
Dim arrService As New ArrayList
Dim objlist As CartProduct
Dim objServiceList As CartService
For Each OrderItem As CartProduct In g_objUserShoppingCart.ListOfItems
objlist = New CartProduct(Global.OrderItem.TypeOfItem.Product)
objlist.ProductId = OrderItem.ProductId
arr.Add(objlist)
Next
''---------------------------------------------------
For Each OrderItem As CartService In g_objUserShoppingCart.ListOfItems
objServiceList = New CartService(Global.OrderItem.TypeOfItem.Service)
objServiceList.ServiceName = OrderItem.ServiceName
arrService.Add(objServiceList)
Next
If Not IsPostBack Then
ProductCartView.DataSource = arr
ProductCartView.DataBind()
ServiceCartView.DataSource = arrService
ServiceCartView.DataBind()
End If
//错误无法转换CartService对象我该如何实现呢,谢谢你
Public Class CartService
Inherits OrderItem
Private m_serviceName As String = ""
Public Sub New(ByVal enuItemType As TypeOfItem)
MyBase.New(enuItemType)
End Sub
Public Property ServiceName() As String
Get Return m_serviceName End Get
Set(ByVal value As String) m_serviceName = value End Set
End Property
End Class