Option Strict On
Public Class Tester
Dim WithEvents Shared acct As Account
Public Shared Sub Main()
Dim warning As MyEventHander = AddressOf Display
Dim bal As Decimal
acct = New Account(200.00d)
bal = acct.Deposit(0.00d)
bal = acct.Debit(400.00d)
bal = acct.Debit(5000.00d)
End Sub
Friend Shared Sub Display(sender As Object,e As MyEventArgs)
Console.WriteLine("Warning: The balance of {0} has fallen below {1}.", _
e.balance, e.WarningLevel)
End Sub
End Class
Public Class MyEventArgs : Inherits EventArgs
Private level As Decimal
Private current As Decimal
Public Sub New(warningLevel As Decimal, currentBalance As Decimal)
Me.level = warningLevel
Me.current = currentBalance
End Sub
Public ReadOnly Property WarningLevel As Decimal
Get
Return level
End Get
End Property
Public ReadOnly Property Balance As Decimal
Get
Return current
End Get
End Property
End Class
Public Delegate Sub MyEventHander(sender As Object, _
e As MyEventArgs)
Public Class Account
Private warningLevel As Decimal
Private balance As Decimal
Public Event MyEvent As MyEventHander
Public Sub New(warningLevel As Decimal)
Me.warningLevel = warningLevel
End Sub
Public Function Deposit(amount As Decimal) As Decimal
balance += amount
RaiseEvent MyEvent(Me, _
New MyEventArgs(warningLevel, balance))
Return balance
End Function
Public Function Debit(amount As Decimal) As Decimal
balance -= amount
RaiseEvent MyEvent(Me, _
New MyEventArgs(warningLevel, balance))
Console.WriteLine("raised")
Return balance
End Function
End Class
raised
raised