本文介绍了购物车显示问题(InvalidCastException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我在下面的这一行中有问题

CartItem = CType(CartEntry.Value,CartItem)

它说-无法将类型为"CartItem"的对象转换为类型为"CartItem".

这是公共购物车商品编码

Well basically im having a problem in this line below

CartItem = CType(CartEntry.Value, CartItem)

It says - Unable to cast object of type ''CartItem'' to type ''CartItem''.

This is the public cart item coding

Public Class CartItem
    Public Product As Productpage
    Public Quantity As Integer

End Class



这是我添加到购物车项目的类



and this is my add to cart item class

Private Sub DisplayCart()
        ListBox1.Items.Clear()
        Dim CartItem As CartItem
        Dim CartEntry As DictionaryEntry
        For Each CartEntry In Cart
            CartItem = CType(CartEntry.Value, CartItem)
            ListBox1.Items.Add(CartItem.Display)
        Next
    End Sub



谢谢您提前提供帮助;-)



Thanks for help in advance ;-)

推荐答案

hiyakaru写道:
hiyakaru wrote:

Dim CartItem作为CartItem

Dim CartItem As CartItem


我希望这只是您错过的逻辑错误...

您正在定义与类名相同的变量!不应这样做...尝试使用除CartItem之外的其他任何变量作为您的变量名.

可以说做为:


I hope its just a logical error you missed...

you are defining a variable that is same as classname! It should not be done... Try anything other than CartItem as your variable name..

Lets say Go ahead and make as:

Dim myCartItem As CartItem


myCartItem = CType(CartEntry.Value, CartItem)


hiyakaru写道:
hiyakaru wrote:

CartItem = CType(CartEntry.Value,CartItem)

CartItem = CType(CartEntry.Value, CartItem)



您想在这里做什么?选角是否正确?检查课程.您可以尝试转换CartEntry.Value吗?



What are you trying to do here? Is the casting correct? Check the classes. Can you convert CartEntry.Value as you are trying to?


这篇关于购物车显示问题(InvalidCastException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 21:12