1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| Imports System
Public Class FireEventArgs
Inherits EventArgs
Public room As String
Public ferocity As Integer
Public Sub New(room As String, ferocity As Integer)
Me.room = room
Me.ferocity = ferocity
End Sub
End Class
Public Class FireAlarm
Delegate Sub FireEventHandler(sender As Object, fe As FireEventArgs)
Public Event FireEvent As FireEventHandler
Public Sub ActivateFireAlarm(room As String, ferocity As Integer)
Dim fireArgs As New FireEventArgs(room, ferocity)
RaiseEvent FireEvent(Me, fireArgs)
End Sub
End Class
Class FireHandlerClass
Public Sub New(fireAlarm As FireAlarm)
AddHandler fireAlarm.FireEvent, AddressOf ExtinguishFire
End Sub
Sub ExtinguishFire(sender As Object, fe As FireEventArgs)
Console.WriteLine()
Console.WriteLine("The ExtinguishFire function was called by {0}.", sender.ToString())
If fe.ferocity < 2 Then
Console.WriteLine("This fire in the {0} is no problem. I'm going to pour some water on it.", fe.room)
Else
If fe.ferocity < 5 Then
Console.WriteLine("Using Fire Extinguisher to put out the fire in the {0}.", fe.room)
Else
Console.WriteLine("The fire in the {0} is out of control. I'm calling the fire department.", fe.room)
End If
End If
End Sub
End Class
Public Class FireEventTest
Public Shared Sub Main()
Dim myFireAlarm As New FireAlarm()
Dim myFireHandler As New FireHandlerClass(myFireAlarm)
myFireAlarm.ActivateFireAlarm("Kitchen", 3)
myFireAlarm.ActivateFireAlarm("Study", 1)
myFireAlarm.ActivateFireAlarm("Porch", 5)
Return
End Sub
End Class |
Partager