1

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
4

1 回答 1

0

Haaaaaaaaaaaaaaa 我找到了......我的一个朋友告诉我要注意事件,他是对的。

我正在使用处理 Holder.ControlAdded 的事件

发送者甚至是持有者本身,我假设它是添加的控件,因此是 InvalidCastError。

错误是在 control.add() 行而不是在事件内部引发的,这导致了我的困惑。

于 2013-06-02T21:02:28.573 回答