What am I doing wrong? - Visual Basic Events
I know that my problem is simple, but I cannot figure out what is wrong
with my code. I have this Head First C# book, and I converted the code in
VB.NET. I expected that the catches subroutine of Pitcher class will be
called after I clicked a button in my form. But nothing happens.
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
After invoking Ball.OnBallInPlay, Pitcher.catches shall listen. Isn't it,
or am I missing something obvious and important?
No comments:
Post a Comment