当我没有尝试施放任何东西时出现错误(Casting error when I'm not trying to cast anything)

编程入门 行业动态 更新时间:2024-10-23 12:31:08
当我没有尝试施放任何东西时出现错误(Casting error when I'm not trying to cast anything)

我正试图让一个简单的纸牌游戏工作......我得到一个“不可能施放”的错误......但我不是想要施展任何东西......我有点混淆为什么我收到这个错误。 下面是我使用的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 Sub

I'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 Class

and 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.

更多推荐

本文发布于:2023-08-07 05:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1460006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   出现错误   东西   cast   Casting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!