Note that there are some explanatory texts on larger screens.

plurals
  1. POretrieve collect of object in listcollection
    primarykey
    data
    text
    <p>I have a class orderItem it is a parent of Product and service class Im adding product or services in a listcollection. How can I cast service or product for retrieving orderitem?</p> <pre><code>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 </code></pre> <hr> <pre><code>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 </code></pre> <hr> <pre><code>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 </code></pre> <hr> <pre><code>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 </code></pre> <hr> <pre><code>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 </code></pre> <p>I'm using this code for retrieving data in the list collection. I'm getiing this error</p> <h2>System.InvalidCastException: Unable to cast object of type 'CartProduct' to type 'CartService'. at Cart.Page_Load(Object sender, EventArgs e) in F:\pic\Eshop\MirraNew\Cart.aspx.vb:line 30</h2> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload