梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
End Class
Class RunningTotal
Inherits IteratorBaseClass
Public Sub New��ByVal collection As IList��Of Integer����
MyBase。New��collection��
Total = 0
End Sub
Protected Overrides Sub ProcessElement��ByVal value As Integer��
Total = Total �� value
End Sub
Public Total As Integer
End Class
Class MaximumValue
Inherits IteratorBaseClass
Public Sub New��ByVal collection As IList��Of Integer����
MyBase。New��collection��
MaxValue = Integer。MinValue
End Sub
´´´´´´´´´´´´´´´´´´´´´´Page 260´´´´´´´´´´´´´´´´´´´´´´´
238 CH AP T E R 9 * L E A R N IN G AB OU T L I ST S�察 �D E L E G A T E S�察 �A N D L A M B DA E X P R E S SI ON S
Protected Overrides Sub ProcessElement��ByVal value As Integer��
If ��value 〃 MaxValue�� Then
MaxValue = value
End If
End Sub
Public MaxValue As Integer
End Class
Module Module1
Sub Main�┌�
Dim elements As IList��Of Integer�� = New List��Of Integer���┌�
elements。Add��1��
elements。Add��2��
elements。Add��3��
Dim runningTotal As Integer = _
TryCast��New RunningTotal��elements��。Iterate�┌��察�RunningTotal��。Total
Dim maximumValue As Integer = _
TryCast��New MaximumValue��elements��。Iterate�┌��察�MaximumValue��。MaxValue
Console。WriteLine�─�RunningTotal �──�& runningTotal & ;��;��
Console。WriteLine�─�Maximum Value �──�& maximumValue & ;��;��
End Sub
End Module
The rewritten code is much longer�察�even though the bolded code�察�which represents the
user code�察�is much shorter。 However�察�this code still isn¨t right。 The code is ill fitting because the
problem that it addresses can be solved using another�察�simpler technique。 So�察�in a nutshell�察�you
can say the problem is that you want to solve a single particular technical problem using an
elegant piece of code that does not include repeated sections that have been copied and pasted。
A better solution is consider the code as two code blocks�察�and in the following section�察�you¨ll
see how delegates can solve the problem of adding and keeping a running total。
WEIGHING THE ADVANTAGES OF REUSING CODE
Very often�察�when you write code�察�the code that performs the task directly is shorter and to the point。 When you
abstract the code and develop general classes�察�the code will begin to bloat and expand�察�but the advantage is
that the code can be reused。 So�察�when is abstracting code worth the effort�拭�
Consider the analogy of building a house。 You are constructing the trusses for the house。 You have a
blueprint that indicates that you need to build 50 trusses。 You could build each of the 50 trusses individually�察 �
or you could build a jig to speed up building the trusses。 And herein lies the problem。 If the trusses can be built
without a jig in 10 hours�察�and with the jig in 2 hours�察�you would think building the jig was a good idea。 But not
so fast。 What if building the jig takes 20 hours�拭�Then the time that you saved by using the jig you lost by
building the jig。
´´´´´´´´´´´´´´´´´´´´´´Page 261´´´´´´´´´´´´´´´´´´´´´´´
C HA P TE R 9 * L E AR N I N G A B O U T L I ST S�察 �DE L E G AT E S �察 �AN D L A M B D A E X PR E SSI O N S 239
Software is no different。 Sometimes�察�even though the code is more plicated and bloated�察�if the code
is reused often enough�察�abstracting it saves time�察�as the end´user code is simplified。 Experience will tell you
when to code specifically or when to write general code that can be reused。 A rule of thumb is to start out by
solving the problem�察�and if it looks like the code can be reused�察�then abstract the specific code。
Using Delegates
Since the beginning of Visual Basic�察�there has been a concept called a delegate。 A delegate is a
type that represents a method signature。 For example�察�consider the following interface definition。
Interface IExample
Sub Method�┌�
End Interface
The equivalent delegate would look like this�此�
Delegate Sub Method�┌�
A delegate and interface can share the same role�察�in that they are two different ways to
represent the signature for a method that you implement elsewhere。 An interface can have
multiple methods and properties。 A delegate is a method signature and can define only the
parameters and return types for that method signature。 The purpose of delegates is to be able
to define a generic method´calling mechanism without needing to add the baggage of imple
menting an interface。
The approach used in the delegate solution to the problem presented in the previous section
is to define a chunk of functionality that performs the iteration�察�called an iterator。 And then to
do something with the iteration�察�another chunk of functionality is integrated via a delegate。
Following is the plete rewritten For Each code that uses delegates。 The entire solution
is shown for a big picture purposes�察�and then will be split into smaller chunks for explanation
purposes。
Imports System。Runtime。pilerServices
Namespace DelegateImplementation
Delegate Sub ProcessValue��ByVal value As Integer��
Module Iterator
' Methods
Public Sub Iterate��ByVal collection As ICollection��Of Integer���察�_
ByVal cb As ProcessValue��
Dim element As Integer
For Each element In collection
cb��element��
Next
End Sub
End Module
´´´´´´´´´´´´´´´´´´´´´´Page 262´´´´´´´´´´´´´´´´´´´´´´´
240 CH AP T E R 9 * L E A R N IN G AB OU T L I ST S�察 �D E L E G A T E S�察 �A N D L A M B DA E X P R E S SI ON S
Module Tests
' Fields
Private _maxValue As Integer
Private _runningTotal As Integer
Private Sub ProcessMaximumValue��ByVal value As Integer��
If ��value 〃 Tests。_maxValue�� Then
_maxValue = value
End If
End Sub
Private Sub ProcessRunningTotal��ByVal value As Integer��
_runningTotal = _runningTotal �� value
End Sub
Public Sub RunAll�┌�
Dim lst As New List��Of Integer���┌�
lst。Add��1��
lst。Add��2��
lst。Add��3��
lst。Add��4��
_runningTotal = 0
Iterator。Iterate��lst�察�New ProcessValue��AddressOf ProcessRunningTotal����
Console。WriteLine�┌─�Running total is �──�& _runningTotal & ;��;����
_maxValue = Integer。MinValue
Iterator。Iterate��lst�察�New ProcessValue��AddressOf ProcessMaximumValue����
Console。WriteLine�┌─�Maximum value is �──�& _maxValue & ;��;����
End Sub
End Module
End Namespace
Declaring the Delegate
The beginning of the code is the single line that contains the Delegate keyword。 Any delegate
implementations must ma