All of
the Employee objects are contained in a collection type, and now you need to iterate over each
employee, applying the raise by calling the Employee.ApplyRaiseOf method:
Imports System
Imports System.Reflection
Imports System.Collections.Generic
CHAPTER 11 n DELEGATES AND EVENTS 224
Delegate Sub ApplyRaiseDelegate(ByVal emp As Employee, _
ByVal percent As Decimal)
Public Class Employee
Private mSalary As Decimal
Public Sub New(ByVal salary As Decimal)
Me.mSalary = salary
End Sub
Public ReadOnly Property Salary() As Decimal
Get
Return mSalary
End Get
End Property
Public Sub ApplyRaiseOf(ByVal percent As Decimal)
mSalary *= 1 + percent
End Sub
End Class
Public Class EntryPoint
Shared Sub Main()
Dim Employees As List(Of Employee) = New List(Of Employee)
Employees.Add(New Employee(40000))
Employees.Add(New Employee(65000))
Employees.Add(New Employee(95000))
'Create open-instance delegate
Dim mi As MethodInfo = GetType(Employee).GetMethod("ApplyRaiseOf", _
BindingFlags.Public Or BindingFlags.Instance)
Dim applyRaise As ApplyRaiseDelegate = _
CType(System.Delegate.CreateDelegate(GetType(ApplyRaiseDelegate), _
mi), ApplyRaiseDelegate)
'Apply raise.
Dim e As Employee
For Each e In Employees
applyRaise(e, CType(0.
Pages:
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371