Prev | Current Page 361 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"


The previous example shows how to create and invoke an open-instance delegate;
however, the delegate could still be more general and more useful in a broad sense. In that
example, you declared the delegate such that it knew it was going to be calling a method on a
type of Employee. Thus at invocation time, you could have placed the call only on an instance
of Employee or a type derived from Employee. You can use a generic delegate to declare the
delegate such that the type on which it is called is unspecified at declaration time.1 Such a
delegate is potentially much more useful. It allows you to state the following: ???I want to represent
a method that matches this signature supported by an as-of-yet unspecified type.??? Only
at the point of instantiation of the delegate are you required to provide the concrete type that
will be called. Examine the following modifications (shown in bold) to the previous example:
Imports System
Imports System.Reflection
Imports System.Collections.Generic
Delegate Sub ApplyRaiseDelegate(Of T)(ByVal instance As T, _
ByVal percent As Decimal)
Public Class Employee
Private mSalary As Decimal
Public Sub New(ByVal salary As Decimal)
Me.mSalary = salary
End Sub
CHAPTER 11 n DELEGATES AND EVENTS 226
1.


Pages:
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
Turcja 1 mimre.benton.pl Biuro księgowe Lublin Fundusze Emerytalne petrozawodzkich.waw.pl