嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及108嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��






                      Return retval 

                  End SyncLock 

              End FunctionEnd Class 



                The bolded code shows that there is still a cast�察�but the type cast is in the method�察�and it  

          uses the  generics parameter declared at the method level。 



           *Note  The SyncLock keyword ensures that the code in this method is not executed by more than one  

          thread at a time。 That way�察�the calling code always gets back the correct sheet。 



           Implementing the Server Spreadsheet 



          Now let¨s look at how the workbook and worksheet are implemented。 I will explain only the  

          important pieces�察�but all of the code is available for download from the Source/Downloads  

           area of the Apress web site ��http��//apress。��。 The class Worksheet��Of BaseType�� imple

          ments the interface IWorksheet�察�but does not specify on which type the worksheet should be  

          based。 In this application�察�we also have a class called TraderBaseClass。 In most applications�察 �

          there is some functionality that most classes will need。 That mon functionality is what I  

           call a domain´specific base class。 In the case of TraderBaseClass�察�that is the implementation of  

          the IDebug interface。 



           Using Lambda Expressions in the Spreadsheet 



          The data members of Worksheet��Of BaseType�� are very similar to the previously defined spread

           sheet class�察�except the declarations are lambda´ready。 Lambda´ready means that you use the  

           Func��Of �� type whenever you want to declare a variable that references a lambda expression。  

          The following three data members are used to store the state of the cell�察�cell calculations that  

          will calculate the state of a cell�察�and cell calculations that calculate the state of cells for an entire  

           column。 



           Private Cells As Func��Of IWorksheet��Of BaseType���察�Integer�察�Integer�察�BaseType���┌撮� 

           Private CellState As BaseType�┌撮� 

           Private ColCells As Func��Of IWorksheet��Of BaseType���察�Integer�察�Integer�察�BaseType���┌� 



                The data member CellState contains the state of the worksheet cell�察�and its type is BaseType�察 �

          meaning that the type of the worksheet cell is whatever BaseType is declared as。 The data members  

           Cells and ColCells are declared as lambda expression references�察�where there are three param

           eters and a return value。 

                Before I continue with the lambda expression explanation�察�I want to shift focus to illustrate  

           a problem。 We are going to play a game of what animal am I�察�where we¨ll use lambda expressions  

          in conjunction with a closure。 The idea is to store the identifier of the animal in a closure and  

          then return it when it¨s requested。 


´´´´´´´´´´´´´´´´´´´´´´Page 325´´´´´´´´´´´´´´´´´´´´´´´

                                                     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 303 



        Dim animals��1�� As Func��Of String�� 

        Dim animal As String 



        animal = ;cow; 

        animals��0�� = Function�┌� animal 



        animal = ;horse; 

        animals��1�� = Function�┌� animal 



        For Each callAnimal In animals 

            Console。Write �─┌──�& callAnimal�┌� & ;��;�� 

        Next 



     The example creates an array of lambda expressions ��animals�� where space for two lambda  

expressions is allocated。 The individual lambda expressions will reference a variable animal�察 �

which contains the animal of the lambda expression。 In the code�察�the lambda expressions are  

assigned when the state of animal changes from cow to  horse。  

     Let¨s say you run the code�察�and Console。WriteLine�┌� generates its output。 What do you  

think the animals will be�拭�Do you expect cow and  horse�察�respectively�拭�Here¨s the output�此�



��horse�� ��horse�� 



     The generated output is not what you would expect。 This demonstrates that lambda expres

sions are stateless。 Yet in earlier explanations of lambda expressions and closures�察�I said that  

variables can be stored in lambda expressions。 Since animal is a value type�察�you would expect  

two copies of animal�察�right�拭�Wrong。 

     Closures do work�察�and variable state is kept across lambda expressions。 The mistake in this  

code is that the same variable is referenced by two lambda expressions。 Thus�察�when the two  

lambda expressions execute�察�they will reference the same animal�察�which was last assigned to be  

a horse。 

      The code that solves the problem uses two unique variables�察�as follows�此�



        Dim animals��1�� As Func��Of String�� 



        Dim animal1 = ;cow; 

        animals��0�� = Function�┌� animal1 



        Dim animal2 = ;horse; 

        animals��1�� = Function�┌� animal2 



        For Each callAnimal In animals 

            Console。WriteLine��callAnimal�┌��� 

        Next 


´´´´´´´´´´´´´´´´´´´´´´Page 326´´´´´´´´´´´´´´´´´´´´´´´

304       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 



               This time when you run the code�察�you get the following output。 



          cow 

          horse 



               Returning to the spreadsheet example�察�the problem is that the behavior just described  

          means this code won¨t work�此�



                      Dim myWorksheet As IWorksheet��Of Double�� 

                      For row As Integer = 0 To 10 

                          myWorksheet。AssignCellCalculation��row�察�1�察�_ 

                              Function��worksheet�察�cellRow�察�cellCol�� _ 

                                    myWorksheet。GetCellState��row�察�0�� _ 

                                    myWorksheet。Calculate��10�察�0���� 

                      Next 



          whereas this code would work�此�



                      Dim myWorksheet As IWorksheet��Of Double�� 

                      For row As Integer = 0 To 10 

                          Dim temp = row 

                          myWorksheet。AssignCellCalculation��row�察�1�察�_ 

                              Function��worksheet�察�cellRow�察�cellCol�� _ 

                                    myWorksheet。GetCellState��temp�察�0�� _ 

                                    myWorksheet。Calculate��10�察�0���� 

                      Next 



               The difference between the two pieces of code is that the second one has the lambda  

          expression that uses the variable declared temp in the context of a loop。 For each iteration of the  

          loop�察�a new instance of temp is allocated�察�and hence each lambda expression has its own instance  

          of the row。 



          Assigning State Without Knowing the Type 



          When using  generics types�察�one of the most mon problems occurs when you need to  

          work with proper types。 In the implementation of the IWorksheet interface�察�it is necessary to  

          implement the AssignCellState�┌� method defined in the interface  IWorksheetSerialize。 

               The explanation of IWorksheetSerialize is slightly plicated and relates to the problem  

          of loading an IWorksheet without knowing the type。 Say that you are saving an  IWorkbook with  

          multiple IWorksheet instances。 Each  IWorksheet instance is a specific type。 When you want to  

          load an  IWorkbook�察�how does the loader know which types there are�拭�The answer is that the  

          loader does not�察�and thus must first load a general type�察�and then make a specific cast。 Take a  

          look at the serialization source code in the project Devspace。Trader。mon and the namespace  

          Devspace。Trader。mon。ServerSpreadsheet。SerializerImpls�察�available as part of the down

          loadable code。 


´´´´´´´´´´´´´´´´´´´´´´Page 327´´´´´´´´´´´´´´´´´´´´´´´

                                                      CH AP T E R   1 1   *    L E A
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��