After adding a handler to the event, it is ready but you must launch its action. To do this, you can use the RaiseEvent operator with the following formula: RaiseEvent EventName() The RaiseEvent operator is required. The EventName placeholder is used to specify the name of the event, and it must be followed by parentheses. Here is an example: Module Exercise Delegate Sub Messenger() Private Event Evidence As Messenger Private Sub ShowMessage() MsgBox("Get the necessary message") End Sub Public Function Main() As Integer AddHandler Evidence, AddressOf ShowMessage RaiseEvent Evidence() Return 0 End Function End Module
The event we used in the previous sections did not take any argument. Just like a delegate, an event can take an argument. The primary rule to follow is that both its delegate and the procedure associated with it must take the same type of event. The second rule is that, when raising the event, you must pass an appropriate event to it. Here is an example: Module Exercise Delegate Sub Messenger(ByVal Msg As String) Private Event Evidence As Messenger Private Sub ShowMessage(ByVal ToShow As String) MsgBox(ToShow) End Sub Public Function Main() As Integer AddHandler Evidence, AddressOf ShowMessage RaiseEvent Evidence("Is this the last thing to do?") Return 0 End Function End Module Just like an event can take an argument, it can also take more than one argument. The primary rules are the same as those for a single parameter. You just have to remember that you are dealing with more than one argument. Here is an example: Module Exercise Delegate Sub Messenger(ByVal Originator As String, ByVal Msg As String) Private Event Evidence As Messenger Private Sub ShowMessage(ByVal Sender As String, ByVal ToShow As String) MsgBox(Sender & ": " & ToShow) End Sub Public Function Main() As Integer AddHandler Evidence, AddressOf ShowMessage RaiseEvent Evidence("Message form the Accounting Department", _ "Remember to do your time sheet") Return 0 End Function End Module Instead of a parameter of a primitive type, you can create an event that takes a class as argument. |
|
|||||
|