编辑列表收集问题:错误说明收集被修改;不能执行枚举操作

本文关键字:不能 执行 枚举 修改 操作 说明 问题 列表 错误 编辑 | 更新日期: 2023-09-27 18:08:06

我有以下类:

Product, Service and OrderItem

ProductService必须继承OrderItem。所以基本上我想把OrderItem对象存储在我的购物车中,这些对象存储在一个列表中。

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
类产品

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

这是我的问题:当用户添加产品时,我想验证产品是否已经存在于列表中。如果有,我想增加这个值,但我有一个错误。这是我的add orderItem代码:

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

编辑列表收集问题:错误说明收集被修改;不能执行枚举操作

最简单的解决方案是使用for循环。我还没有测试过这段代码,但它应该是这样的:

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。净

EDIT:当找到匹配的ProductID时将Continue For更改为Exit For

当找到匹配的ProductID时,听起来您需要跳出循环,所以试试这个:

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