我正试图让一个简单的纸牌游戏工作......我得到一个“不可能施放”的错误......但我不是想要施展任何东西......我有点混淆为什么我收到这个错误。 下面是我使用的3个对象的快速摘要。
Public Class Holder inherits Panel end class Public Class Card inherits PictureBox end class public Class Location Public Property X as integer Public Property Y as integer end class Public Class Board inherits Form Public Property Tile as Dictionnary(of Location, Holder) Public sub PlayCard(card as Card, loc as Location) Tile(loc).Controls.Add(card) end sub end Class我得到一个InvalidCastException:
无法在类型'卡'中投射对象类型'持有人'。
我已经进行了三次检查,以确保它真的是一张卡片作为参数传递给PlayCard sub ....我不知道该怎么做...
有什么建议么 ?
编辑:
Private Sub TEST() Handles Board.Load Dim c = New Card Dim p1 As Location p1 = New Location(2, 1) c.Image = My.Resources.Tree Me.Play(c, p1) End SubI'm trying to get a simple card game to work ... and I'm getting an "Impossible to cast " error ... but I'm not trying to cast anything ... I'm a little confuse about why I get this error. Heres a quick summary of the 3 object I use.
Public Class Holder inherits Panel end class Public Class Card inherits PictureBox end class public Class Location Public Property X as integer Public Property Y as integer end class Public Class Board inherits Form Public Property Tile as Dictionnary(of Location, Holder) Public sub PlayCard(card as Card, loc as Location) Tile(loc).Controls.Add(card) end sub end Classand I get an InvalidCastException:
Impossible to cast objet type 'Holder' in Type 'Card'.
I've triple check to make sure its really a card thats being pass as a parameter to the PlayCard sub .... and I get no idea what to do from here ...
any suggestions ?
EDIT :
Private Sub TEST() Handles Board.Load Dim c = New Card Dim p1 As Location p1 = New Location(2, 1) c.Image = My.Resources.Tree Me.Play(c, p1) End Sub最满意答案
Haaaaaaaaaaaaaaa我发现它...我的一个朋友告诉我要注意事件,他是对的。
我正在使用Handles Holder.ControlAdded的事件
甚至是Holder本身的发送者,我假设它是添加的控件,因此InvalidCastError。
在control.add()行而不是在事件内部引发错误,这导致我的困惑。
Haaaaaaaaaaaaaaa I find it ... a friend of mine told me to watch out for events and he was right.
I'm using an event that Handles Holder.ControlAdded
The sender of that even is the Holder itself, I had assume it was the control added, hence the InvalidCastError.
the error get raised on the control.add() line instead of inside the event, which leads to my confusion.
更多推荐
发布评论