Prev | Current Page 362 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

The next chapter covers generics in more detail.
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(Of Employee) = _
CType([Delegate].CreateDelegate( _
GetType(ApplyRaiseDelegate(Of Employee)), mi), _
ApplyRaiseDelegate(Of Employee))
'Apply raise.
Dim e As Employee
For Each e In Employees
applyRaise(e, CType(0.1, Decimal))
'Send new salary to console.
Console.WriteLine("Employee's new salary = {0:C}", e.Salary)
Next
End Sub
End Class
Now, the delegate is much more generic. Consider an imaging program that supports
applying filters to various objects on the canvas. Suppose you need a delegate to represent a
generic filter type that, when applied, is provided a percentage value to indicate how much of
an effect it should have on the object.


Pages:
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
Tango Olsztyn sprzedaż mieszkań lublin życzenia z okazji urodzin szkolenia bhp warszawa texas holdem