Reputation: 347
I have the following classes:
Product, Service and OrderItem
Product
and Service
must inherit OrderItem
. So basically I want to store OrderItem
object in my shopping cart and these object are store in a list.
Public MustInherit Class OrderItem
Private m_enuItemType As TypeOfItem = TypeOfItem.None
Private m_strUserID As Integer
Private m_UserName As String
Private m_Currency As String
Private m_CurrencyType As Decimal
Private m_MiniCartSubTotal As Decimal
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
End Class
Class Product
Public Class CartProduct
Inherits OrderItem
Private Product_Id As String = ""
Private Qty As Integer = Nothing
Private price As String = ""
Private priceFormat As String = ""
Private UserPrice As Decimal = 0
Public Sub New(ByVal enuItemType As TypeOfItem)
MyBase.New(enuItemType)
End Sub
Public Property ProductId() As String
Get
Return Product_Id
End Get
Set(ByVal value As String)
Product_Id = value
End Set
End Property
End Class
Here is my problem: When user add product, I want to verify if the product already exist in the List. If it does, I want to increment the value but I'm having an error. Here is my add orderItem code:
Public Function AddItem(ByVal objOrderItem As OrderItem) As Boolean
Try
If m_objArrListOfItems.Count > 0 Then
' Validate item to check if it is already in the list
' If yes, increment counter, otherwise add new item to the list
Dim objValidateCartProduct As New CartProduct(OrderItem.TypeOfItem.Product)
Select Case objOrderItem.ItemType
Case OrderItem.TypeOfItem.Product
objValidateCartProduct = DirectCast(objOrderItem, CartProduct)
End Select
For Each p As CartProduct In m_objArrListOfItems
If objValidateCartProduct.ProductId = p.ProductId Then
p.OrderQty = p.OrderQty + objValidateCartProduct.OrderQty
Else
m_objArrListOfItems.Add(objOrderItem)
End If
Next
Else
m_objArrListOfItems.Add(objOrderItem)
End If
Catch ex As Exception
MsgBox(ex.ToString)
' Log error
End Try
End Function
Upvotes: 0
Views: 122
Reputation: 46067
The easiest solution is to use a for loop for this kind of stuff. I haven't tested this code, but it should go something like this:
for (int i = 0; i < m_objArrListOfItems.Count; i++)
{
Product p = m_objArrListOfItems[i];
if (objValidateCartProduct.ProductID == p.ProductID)
{
p.OrderQty += objValidateCartProduct.OrderQty;
break;
}
m_objArrListOfItems.Add(objOrderItem);
}
VB.NET
EDIT: Changed Continue For
to Exit For
when a matching ProductID
is found.
It sounds like you need to break out of the loop when a matching ProductID
is found, so try this:
For i As Integer = 0 To m_objArrListOfItems.Count - 1
Dim p As Product = m_objArrListOfItems(i)
If objValidateCartProduct.ProductID = p.ProductID Then
p.OrderQty += objValidateCartProduct.OrderQty
Exit For
End If
m_objArrListOfItems.Add(objOrderItem)
Next
Upvotes: 1