我知道我的问题很简单,但我无法弄清楚我的代码有什么问题。我有这本 Head First C# 书,我在 VB.NET 中转换了代码。我希望在我单击表单中的按钮后调用类的catches
子例程。Pitcher
但什么也没有发生。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myBall As New Ball
Dim pitcher As New Pitcher
myBall.OnBallInPlay(New BallEventArgs(10, 20))
End Sub
End Class
Public Class Ball
Public Event BallInPlay(ByVal sender As Object, ByVal e As BallEventArgs)
Public Sub OnBallInPlay(ByVal e As BallEventArgs)
RaiseEvent BallInPlay(Me, e)
End Sub
End Class
Public Class BallEventArgs
Inherits EventArgs
Dim trajectory As Integer
Dim distance As Integer
Public Sub New(ByVal trajectory As Integer, ByVal distance As Integer)
Me.trajectory = trajectory
Me.distance = distance
End Sub
End Class
Public Class Pitcher
Public WithEvents mySender As Ball
Public Sub catches(ByVal sender As Object, ByVal e As EventArgs) Handles mySender.BallInPlay
MessageBox.Show("Pitcher: ""I catched the ball!""")
End Sub
End Class
调用后Ball.OnBallInPlay
,Pitcher.catches
应听。不是吗,还是我遗漏了一些明显而重要的东西?