检索listcollection中的对象集合

时间:2011-07-27 12:27:45

标签: asp.net vb.net

我有一个类orderItem它是产品和服务类的父级我在listcollection中添加产品或服务。如何投射服务或产品以检索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

我正在使用此代码检索列表集合中的数据。我正在纠正这个错误

System.InvalidCastException:无法将“CartProduct”类型的对象强制转换为“CartService”类型。在Cart.Page_Load(对象发件人,EventArgs e)在F:\ pic \ Eshop \ MirraNew \ Cart.aspx.vb:第30行

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

1 个答案:

答案 0 :(得分:0)

您的问题是可变名称重复使用。您在这里使用相同的变量名称OrderItem,用于两种不同的类型。虽然在您看来,变量的作用域是该块,但是通过为它指定一个类型,您已经在方法级别修复了它。

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

有两个解决方案,要么删除类型声明(可能在旧版本的VB中不起作用),要么只重命名循环变量。

1)

For Each OrderItem In g_objUserShoppingCart.ListOfItems
    objlist = New CartProduct(Global.OrderItem.TypeOfItem.Product)
    objlist.ProductId = OrderItem.ProductId
    arr.Add(objlist)
Next

For Each OrderItem In g_objUserShoppingCart.ListOfItems
    objServiceList = New CartService(Global.OrderItem.TypeOfItem.Service)
    objServiceList.ServiceName = OrderItem.ServiceName
    arrService.Add(objServiceList)
Next

2)

For Each pOrderItem As CartProduct In g_objUserShoppingCart.ListOfItems
    objlist = New CartProduct(Global.OrderItem.TypeOfItem.Product)
    objlist.ProductId = pOrderItem.ProductId
    arr.Add(objlist)
Next

For Each sOrderItem As CartService In g_objUserShoppingCart.ListOfItems
    objServiceList = New CartService(Global.OrderItem.TypeOfItem.Service)
    objServiceList.ServiceName = sOrderItem.ServiceName
    arrService.Add(objServiceList)
Next

下面是您问题的更通用示例。这将编译但失败并出现相同的基本错误。

    Dim Ints As New List(Of Integer)
    Dim Strings As New List(Of String)
    Ints.Add(1)
    Strings.Add("Bob")
    For Each V As Integer In Strings
        Trace.WriteLine(V)
    Next

    For Each V As String In Ints
        Trace.WriteLine(V)
    Next

此外,当你在那里时,改变子类的定义,调用者甚至不需要知道枚举:

Public Class Product
    Inherits OrderItem

    Public Sub New()
        MyBase.New(Global.OrderItem.TypeOfItem.Product)
    End Sub

End Class