嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈��
源平慎弌傍利 卦指云慕朕村 紗秘慕禰 厘議慕尺 厘議慕禰 TXT畠云和墮 〆辺茄欺厘議箝誓匂〇

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及55何蛍

酔楯荷恬: 梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈 梓囚徒貧議 Enter 囚辛指欺云慕朕村匈 梓囚徒貧圭�鮗� ● 辛指欺云匈競何! 泌惚云慕短嗤堋響頼���誅卒亮茂�俊彭堋響��辛聞喘貧圭 "辺茄欺厘議箝誓匂" 孔嬬 才 "紗秘慕禰" 孔嬬��



          Managed�察�which references the variable _managed。 The idea behind this class is to assign an  

           object in the constructor that can be referenced using the property。 The class Container does  

          not know what the variable _managed does or its capabilities。 Container does not care�察�because  

           Container is acting like a basket that holds an instance of whatever is given to it。 

                The Container class could be used as follows�此�



           Dim container As Container = New Container��New MyType�┌��� 



          TryCast��container。Managed�察�MyType��。Method�┌� 



                When Container is instantiated�察�the _managed data member is assigned an instance of  

          MyType。 MyType is a type that is used for illustrative purposes and has a single method�察�Method�┌�。  

          To retrieve and use the managed type�察�the property Managed is referenced。 However�察�the  

           method Method�┌� cannot be called directly because the property Managed is of type Object�察�and  

          thus you need to cast the property to type MyType so that the call to Method�┌� is legal。 

                The cast using TryCast will result in a valid instance of MyType or a Nothing value�察�resulting  

           in a null object reference exception。 Here¨s a safe way of using the Managed property�此�



           Dim container As Container = New Container��New MyType�┌��� 



           If TypeOf container。Managed Is MyType Then 

              TryCast��container。Managed�察�MyType��。Method�┌� 

           End If 



                The bolded code is the addition of the If block to test whether the container references the  

          type MyType。 Not shown is what the code should do if the property Managed is not MyType。 The  

          fact that you need to verify that the container references the correct type�察�and you also need to  

          think of an alternative plan if the type is incorrect�察�adds quite a bit of code。 It¨s like that sentence  

           about the duck!sure�察�you get the general idea of what is being said�察�but are you 100�ァ�sure of  

          the meaning�拭�


´´´´´´´´´´´´´´´´´´´´´´Page 309´´´´´´´´´´´´´´´´´´´´´´´

                                                       CH AP T E R   1 1   *    L E A R N IN G   AB O U T   。 N E T  G E N E R I CS 287 



     Now look at the following code�察�which implements a container using  generics。 



    Public Class GenericsContainer��Of ManagedType�� 

        Private _managed As ManagedType 



        Public Sub New��ByVal toManage As ManagedType�� 

            _managed = toManage 

        End Sub 



        Public ReadOnly Property Managed�┌� As ManagedType 

            Get 

                Return _managed 

            End Get 

        End Property 

    End Class 



     You can write code that uses  generics and code that provides types based on   

generics。 The definition of GenericsContainer demonstrates code that provides types based on  

 generics。 You¨ll see the code that uses  generics next。  

      generics parameters are associated with types�察�such as classes and interfaces�察�or with  

methods。 In the case of GenericsContainer�察�the  generics parameter ManagedType is a  

placeholder for an actual type。 



*Note  monly�察�developers use the notation of a single letter when defining a  generics parameter。  

I am not a fan of that notation�察�because it tells me nothing�察�especially when there are multiple parameters。 I  

remend using an identifier that describes what the parameter does�察�appended with the word Type�察�to  

indicate a  generics parameter is being defined。 



     With GenericsContainer�察�ManagedType is used as an identifier in the place of the identifier  

object in the type Container。 This is a rule of thumb。 Whenever you find yourself using an object  

generically to define a type�察�you probably could use  generics。 Think of  generics  

types as general things。 

     The following code demonstrates how to use GenericsContainer with MyType。 



Dim container As GenericsContainer��Of MyType�� = _ 

    New GenericsContainer��Of MyType����New MyType�┌��� 

container。Managed。Method�┌� 



     When instantiating GenericsContainer�察�notice how the identifier ManagedType must be  

replaced with an identifier that represents an already existing type。 This identifier replacement  

results in a new and unique type ��also called a concrete type��。 The advantage of  generics is  

that when you provide a concrete type�察�you don¨t need to check to make sure that everything is  

correct。 

     The  runtime will generate a type that has an intent similar to the following code。 


´´´´´´´´´´´´´´´´´´´´´´Page 310´´´´´´´´´´´´´´´´´´´´´´´

288        CH AP T E R   1 1   *    L E A R N I N G   A B OU T   。 N E T  G E N E R I CS 



           Public Class GenericsContainerMyType  

               Dim _managed As MyType 



               Public Sub New��ByVal toManage As MyType��  

                   _managed = toManage 

               End Sub 



               Public ReadOnly Property Managed�┌� As MyType  

                   Get  

                       Return _managed 

                   End Get 

               End Property 

           End Class 



                Visual Basic piles a  generics type as an inplete type。 When the inplete  

           type is concretized�察 �creates a brand´new type�察�and does this without requiring the devel

           oper to do anything in particular。 This means that if you use GenericsContainer with 15 different  

           types�察 �will generate 15 definitions of GenericsContainer while the program is executing。 



                                             ABSTRACTION AND  GENERICS 



              With  generics�察�you can verify and ensure that everything is being said properly�察�with the cost being  

              plexity。 Consider the sentence that clearly described the duck。 To produce it�察�you need to have learned  

              additional rules regarding grammar。 When using  generics�察�you need to master creating abstractions。 

                    generics are an abstraction。 Just as interfaces are an abstraction of classes�察 �generics are an  

              abstraction above interfaces。 Interfaces define an intention�察�and  generics define an abstract implemen

              tation of an intention。 

                   What is challenging with  generics is getting your thoughts together into an abstract intention  

              implementation。 It is like writing a document!you write it once�察�read it over�察�rewrite it�察�read it over�察�and  

              rewrite it again。 With  generics�察�you are gathering thoughts together into a general plan of action。 This  

              is why some people are pletely confused and don¨t understand  generics。 Writing your own   

              generics code requires some forethought。 



           The Theory of a Server´Side Spreadsheet 



           The example in this chapter is a spreadsheet for security traders。 When you trade securities! 

           whether they are equities�察�bonds�察�options�察�or futures!you will be confronted with information  

           overload。 You might have seen pictures of traders with desks full of desktop monitors。 A trader  

           might have seven to eight monitors displaying various bits of information。 A trader is a very  

           specialized type of domain that requires its own ways of processing information。 One aspect  

           that makes writing applications for traders difficult is that the nature of the data constantly  

           changes�察�and types get more in the way than they help。 As a result�察�traders adore spreadsheets。 

                 Spreadsheets are useful because they can process large amounts of information in a relatively  

           ad hoc manner。 However�察�one downside to spreadsheets is that the processing time can  


´´´´´´´´´´´´´´´´´´´´´´Page 311´´´´´´´´´´´´´´´´´´´´´´´

                                                      CH AP T E R   1 1   *    L E A R N IN G   AB O U T   。 N E T  G E N E R I CS 289 



dramatically increase due to the constant pushing and pulling of the data to and from the  

spreadsheet。 To speed up processing�察�we will define and implement a spreadsheet that has the  

advantages of a traditional client´side spreadsheet。 



*Note  The theory and solution presented here are specific to the domain of trading�察�where the cost of hard

ware is well worth the ability to trade properly。 Therefore�察�specific design aspects assume that you have the  

latest and greatest hardware。 



     An initial attempt at a spreadsheet would be the following code that uses  generics�此�



Class Spreadsheet 

    Public Cells As Func��Of Object���┌撮� 

    Public State As Object�┌撮� 



    Public Sub New�┌� 

        Cells = New Func��Of Object����10�察�10�� ���� 

        State = New Object��10�察�10�� ���� 

    End Sub 



    Public Sub Execute�┌� 

        For col As Integer = 0 To Cells。GetLength��1�� 1 

            For row As Integer = 0 To Cells。GetLength��0�� 1 

                Console。WriteLine��col & ; ; & ; ; & row�� 

                If Cells��row�察�col�� IsNot Nothing Then 

                    State��row�察�col�� = Cells��row�察�col���┌� 

                End If 

            Next 

        Next 



    End Sub 

End Class 



     The sample spreadsheet is defined using the data members Cells and State。 Both data  

members are arrays with two dimensions。 The first dimension represents the rows�察�and the  

second dimension represents the columns。 You could define as many dimensions as you wish�察 �

but for the scope of the server spreadsheet�察�we take a two´dimensional approach。  

     The  Execute�┌� method goes through the individual rows and columns of the Cells data  

member�察�calculates the state of the cell�察�and assigns the state to the State data member。 The  

data member Cells represents a function that is executed to generate the result of a particular  

cell that is assigned to the data member State。 Both data members store and manipulate Objects�察 �

which makes the spreadsheet flexible。 However�察�a gain in one aspect means a reduction in  

another aspect�察�in this case�察�the loss is in performance。 But performance is what algorithmic  

trading software cannot sacrifice�察�and native types would be best。  

     To make the spreadsheet perform as fast as it can�察�we need to use fixed´dimension arrays。  

However�察�with fixed´dimension arrays�察�we are moving away from a traditional object´oriented  


´´´´´´´´´´´´´´´´´´´´´´Page 312´´´´´´´´´´´´´´´´´´´´´´´

290       CH AP T E R   1 1   *    L E A R N I N G   A B OU T   。 N E T  G E N E R I CS 



           approach。 You could argue that spreadsheets are not object´oriented at all and are a problem  

          with respect to programmability。 I would agree with that ment�察�but spreadsheets solve one  

           class of problems very elegantly。 In the case of financial trading software�察�they solve the problem of  

           managing very large amounts of data efficiently。  



           *Note  Object´oriented code is maintainable and extendable。 However�察�object´oriented code can be slow。 I  

           have done tests where I found fixed´dimension arrays perform two to three times faster than the equivalent  

           object´oriented application。 But performance is not always the primary consideration。 Also�察�fixed´dimension  

           arrays will not always give you the desired performance boost�察�because other parts of your code might be  

           much slower。 Therefore�察�generally�察�you should not use fixed´dimension arrays。 



                The Cells data member is a delegate�察�or lambda expression�察�that is defined using code similar  

           to the following。 When we want to fill a cell�察�we call the appropriate function on CellFactories�察 �

          which in turn returns the lambda expression that represents the new value stored in the cell�此�



           Module CellFactories 

               Public Function DoAdd��ByVal cell1 As Func��Of Object���察�_ 

                                     ByVal cell2 As Func��Of Object���� As Func��Of Object�� 

                   Return Function�┌� CType��cell1�┌��察�Double�� �� CType��cell2�┌��察�Double�� 

               End Function 



               Public Function DoMultiply��ByVal cell1 As Func��Of Object���察�_ 

                                          ByVal cell2 As Func��Of Object���� As Func��Of Object�� 

                   Return Function�┌� CType��cell1�┌��察�Double�� * CType��cell2�┌��察�Double�� 

               End Function 



               Public Function FixedValue��ByVal value As Object�� As Func��Of Object�� 

                   Return Function�┌� value 

               End Function 

           End Module 



                The lambda expressions can be used to add two cells together�察�multiply two cells together�察 �

           or store a fixed value。 With the lambda expressions and the spreadsheet�察�you have two pieces  

           of source code that�察�when bined�察�have the ability to solve plicated problems。 The key  

           idea that you need to take away is that the Spreadsheet class and the lambda expressions defined in  

           CellFactories do not know about each other。 The lambda expressions could be used in a context  

           other than a spreadsheet。 The only requirement is that the function types and signatures match。 

                The sample spreadsheet that would be used to add and multiply some cells together would  

           be as follows�此�



           Dim spreadsheet As Spreadsheet = New Spreadsheet�┌� 



           spreadsheet。Cells��1�察�0�� = CellFactories。FixedValue��10。0�� 

           spreadsheet。Cells��0�察�1�� = CellFactories。FixedValue��10。0�� 


´´´´´´´´´´´´´´´´´´´´´´Page 313´´´´´´´´´´´´´´´´´´´´´´´

                                                       CH AP T E R   1 1   *    L E A R N IN G   AB O U T   。 N E T  G E N E R I CS 291 



spreadsheet。Cells��1�察�2�� =  _ 

    CellFactories。DoAdd��spreadsheet。Cells��1�察�0���察�spreadsheet。Cells��0�察�1���� 

spreadsheet。Cells��2�察�2�� = CellFactories。DoMultiply��spreadsheet。Cells��1�察�2���察�_ 

    CellFactories。FixedValue��2。0���� 

spreadsheet。Execute�┌� 



Console。WriteLine�─�Contents of ��1�察�2���此 ─�& spreadsheet。State��1�察�2��。ToString�┌��� 

Console。WriteLine�─�Contents of ��2�察�2���此 ─�& spreadsheet。State��2�察�2��。ToString�┌��� 



     The sample code illustrates a rudimentary example of a spreadsheet and how lambda  

expressions can be used。 In this chapter�察�the focus will be on how to create a spreadsheet  

implementation that is effective�察�mostly object´oriented�察�and maintainable。 



Architecting a Server´Side Spreadsheet  



To architect a server spreadsheet�察�the following requirements must be met�此�



     o  Performance�此�Wherever possible�察�the design should not sacrifice performance。 



     o  Usability�此�The server´side spreadsheet must be easy to program from a Visual Ba
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��16�� 家��13��
酔楯荷恬: 梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈 梓囚徒貧議 Enter 囚辛指欺云慕朕村匈 梓囚徒貧圭�鮗� ● 辛指欺云匈競何!
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��