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

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

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






              Public Function Destroy�┌� As String Implements IExtendedProcessor。Destroy 

                  Dim builder As New StringBuilder�┌� 

                  Dim c1 As Integer 

                  For c1 = 1 To 46 1 

                      builder。Append�─�Number �──�& c1 & ;�� Found �─┌� 

                      Dim foundCount As Integer = 0 

                      builder。Append�┌┌�foundCount �� FrequencyOfANumber��c1���� & ;��; & ChrW��10������ 

                  Next c1 

                  Return builder。ToString�┌� 

              End Function 



              Private Function FrequencyOfANumber��ByVal numberToSearch As Integer�� As Integer 

                  Dim query = From ticket In _tickets _ 

                                  Where ticket。Numbers��0�� = numberToSearch _ 

                                  Or ticket。Numbers��1�� = numberToSearch _ 

                                  Or ticket。Numbers��2�� = numberToSearch _ 

                                  Or ticket。Numbers��3�� = numberToSearch _ 

                                  Or ticket。Numbers��4�� = numberToSearch _ 

                                  Or ticket。Numbers��5�� = numberToSearch _ 

                                  Select ticket。Numbers 

                  Return query。Count�┌� 

              End Function 


´´´´´´´´´´´´´´´´´´´´´´Page 423´´´´´´´´´´´´´´´´´´´´´´´

                                                              CH AP T E R   1 5   *    L E A R N I N G   A B OU T   L I N Q 401 



    Public Function Initialize�┌� As String Implements IExtendedProcessor。Initialize 

        Return ;; 

    End Function 



    Public Function Process��ByVal input As String�� As String _ 

      Implements IProcessor。Process 

        Dim reader As TextReader = New StringReader��input�� 

        Do While reader。Peek  ´1 

            Dim splitUpText As String�┌� = reader。ReadLine。Split��New Char�┌� ��; ;c���� 

            Dim dateSplit As String�┌� = splitUpText��0��。Split��New Char�┌� ��;。;c���� 

            Dim ticket As New Ticket�─�_ 

                                     New DateTime�─�_ 

                                       Integer。Parse��dateSplit��0�����察�_ 

                                       Integer。Parse��dateSplit��1�����察�_ 

                                       Integer。Parse��dateSplit��2�������察�_ 

                                       New Integer�┌� �� _ 

                                        Integer。Parse��splitUpText��1�����察�_ 

                                        Integer。Parse��splitUpText��2�����察�_ 

                                        Integer。Parse��splitUpText��3�����察�_ 

                                        Integer。Parse��splitUpText��4�����察�_ 

                                        Integer。Parse��splitUpText��5�����察�_ 

                                        Integer。Parse��splitUpText��6�������察�_ 

                                        Integer。Parse��splitUpText��7������ 

            _tickets。Add��ticket�� 

        Loop 

        Return ;; 

    End Function 

End Class 



     Let¨s examine how the implementation works。 



Borrowing Code to Solve Another Problem 



The borrowed code is the implementation of the  Process�┌� method�察�shown here in abbrevi

ated form�此�



    Public Function Process��ByVal input As String�� _ 

        As String Implements IProcessor。Process 

        Dim reader As TextReader = New StringReader��input�� 

        Do While reader。Peek  ´1 

            Dim splitUpText As String�┌� = reader。ReadLine。Split��New Char�┌� ��; ;c���� 

            Dim dateSplit As String�┌� = splitUpText��0��。Split��New Char�┌� ��;。;c���� 

            Dim ticket As New Ticket�─�_ 

          '。 。 。 

        Loop 

        Return ;; 

    End Function     


´´´´´´´´´´´´´´´´´´´´´´Page 424´´´´´´´´´´´´´´´´´´´´´´´

402        CH AP T E R   1 5   *    L E A R N I N G   A B OU T   L I N Q 



                  Other than the new _tickets。Add��ticket�� line�察�the code is identical to the Text2Binary。 

            Process�┌� implementation。 The bolded code adds the lottery draw to the list of drawn numbers。  

            Once the tickets have been instantiated�察�they can be added to the list of tickets that will be  

            queried and searched。 



                                                                 CODE REUSE 



               The borrowed code demonstrates code reuse through copy and paste。 Realize that code that was used to  

               process binary objects is now being used in a pletely new context。  

                     The copied and pasted code reuses classes and functionality from another problem context。 You copied  

               and pasted the functionality to parse and instantiate the Ticket type�察�but reused the Ticket type itself。  

                     When we worked on the code for the Ticket type�察�you probably had no idea that we would reuse the  

               same code。 In fact�察�even I had no idea that we would reuse the code。 I find this happens a lot in my projects。 

                     So how does this work�拭�I write code to fulfill two criteria�此�minimal to solve the task and general enough  

               to not restrict further usage。 So I don¨t actually design for code reuse�察�and I don¨t have code reuse on my mind。  

               What I have on my mind is to design code in such a way that it could potentially be reused。 

                     Let¨s look at that code that could have solved the frequency problem previously。 



               Dim splitUpText�┌� as String = lineOfText。Split��New Char�┌� �� ; ;c ���� 

               frequency��Integer。Parse��splitUpText��0������ ��= 1 

               frequency��Integer。Parse��splitUpText��1������ ��= 1 

               。 。 。 



                     Is this code efficient�拭�No�察�even though the lines of code are minimal�察�the code itself is not efficient。 If I  

               wanted to use the same code to perform another frequency analysis�察�which could happen�察�I would need to  

               copy and paste yet again�察�and thus the code is not efficient。  

                     Is this code general enough to be used in another context�拭�Absolutely not�察�because to reuse the code�察 �

               you would need to copy and paste it�察�and do some slight alterations。  

                     This is an excellent example of code that can be written very quickly and is very effectively used by copying  

               and pasting it everywhere。 You are productive and can solve a problem quickly�察�but it cannot be easily extended or  

               maintained。 Imagine finding a bug and having copied and pasted the code ten times。 That would mean you  

               would need to find the ten different locations and see if the bug exists in those different locations。 



            Using LINQ 



            To find the frequency of a specific number�察�you don¨t need to use LINQ。 In fact�察�LINQ can  

            always be avoided by using Visual Basic code。 So then why use LINQ�拭�The reason is that LINQ  

            makes it easier for you to write plicated search queries that are agnostic of the source。 An  

            example is two versions of the code used to solve the frequency problem�此�one that is not reus

            able and one that is reusable。 The code that is not reusable is the query without LINQ�察�and the  

            reusable code is the query with LINQ。 

                  So let¨s look at the frequency code that is not reusable。 


´´´´´´´´´´´´´´´´´´´´´´Page 425´´´´´´´´´´´´´´´´´´´´´´´

                                                                CH AP T E R   1 5   *    L E A R N I N G   A B OU T   L I N Q 403 



Function FrequencyOfANumberNotReusable��ByVal numberToSearch As Integer�� _ 

  As Integer 

    Dim runningTotal As Integer = 0 

    For Each aTicket as Ticket in _tickets 

        If aTicket。Numbers��0�� = numberToSearch OrElse _ 

          aTicket。Numbers��1�� = numberToSearch OrElse _ 

          aTicket。Numbers��2�� = numberToSearch OrElse _ 

          aTicket。Numbers��3�� = numberToSearch OrElse _ 

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