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

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

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




                          Dim newDate As String = _ 

                              dateSplit��0�� & ;。; & dateSplit��1�� & ;。; & dateSplit��2�� 

                          If _dates。Contains��newDate�� Then 

                              Continue Do 

                          End If 

                          _dates。Add��newDate�� 

                          retval。Append��newDate�� 

                          For c1 As Integer = 0 To 7 

                              retval。Append�── ─�& splitUpText��c1���� 

                          Next 

                      Else 

                          _dates。Add��splitUpText��0���� 

                          retval。Append��lineOfText�� 

                      End If 



               Of all the problems we needed to solve�察�this is the trickiest�察�because it required multiple  

          pieces of code in multiple places。 The code processes the data stream and keeps a list of dates。  

          A date is added to the list only if the date does not exist in the list�察�which is checked using the  

          Contains�┌� method。 The  lists expose this method to verify if the object being passed to it  

          equals one of the list members。 The way that most lists implement the method is to iterate each  

          method and call the Equals�┌� method。 The catch is that if you declare a custom type�察�the  Equals�┌�  

          method defaults to verifying if one reference value equals another。 You would need to imple

          ment a custom  Equals�┌� method。  



          *Note  When you encounter situations where you need to write multiple pieces of code in multiple places�察 �

          that is a sign that you are about to create code that is hard to maintain。 You should take a close look at the  

          code�察�and If possible�察�rewrite it。 Of course�察�this depends on a lot of factors。 So sometimes�察�you will need to  

          just write the code�察�test it�察�and hope that it works。 



               With the final solution�察�the console application TextProcessor is plete。 You could  

          process lotto。txt and get a correctly formatted data stream。 



          Piping Binary Data 



          When working with the console�察�for the most part�察�you will be transferring text data from one  

          process to another or from one file to another file。 However�察�when developing in �察�working  


´´´´´´´´´´´´´´´´´´´´´´Page 291´´´´´´´´´´´´´´´´´´´´´´´

                                                            CH A PT E R   1 0   *    L E A R N I N G   A B O U T  P E R S IS T E N CE 269 



with text is not always the best choice。 A more efficient approach might be to store the data in  

a binary format。  

      The easiest way to understand the difference between text data and binary data is to open  

a binary file using a text editor�察�as shown in Figure 10´8。 



Figure 10´8。 A binary file in a text editor 



      In Figure 10´8�察�you see just a bunch of funny characters with some text scattered throughout。  

A binary file is different from a text file in that the format of a binary file is whatever the program  

that reads and writes the binary data decides。 The advantage of using a binary file is that you  

can create plex structures。 The disadvantage is that only the developer of the reader/writer  

program knows what the file contains。 

      Binary files are not always smaller�察�but they are more efficient because you do not need to  

parse and process the data。 For example�察�in the text´based lottery data stream reader�察�we needed  

to parse every single line and then split the line of text into individual fields�察�which were then  

parsed as integers and assigned to a variable。 Using a binary file�察�you only need to create a  

binary type�察�and read or write that type。 

      In this section�察�we will continue with the example of the lottery´prediction application�察�but  

this time�察�create it as a console application that converts a text data stream into a binary data  

stream and then back to a text stream。 You¨ll see how you can pipe the data from one stream to  

another by sending it from one application to another application。 When we are finished  

building this application�察�the following mand line will be valid。 



type lotto。txt �� TextProcessor。exe �� Text2Binary。exe �� Binary2Text。exe 



      The mand line starts with generating a text data stream using the type mand。  

The mand TextProcessor。exe generates a clean data stream that is then piped into  


´´´´´´´´´´´´´´´´´´´´´´Page 292´´´´´´´´´´´´´´´´´´´´´´´

270       CH AP T E R   1 0   *    L E A R N I N G   A B OU T   P E R S IS TE N CE 



           Text2Binary。exe�察�which generates a binary data stream。 Finally�察�the binary data stream is  

           converted back into a text stream using Binary2Text。exe�察�which displays the data on the console。  

                The architecture of TextProcessor with its implementation of the  IProcessor interface  

           and a general architecture of grabbing a file or console input/output stream worked。 However�察 �

           IProcessor is not usable for binary streams�察�so we need to implement a new interface and  

           architecture。 



           *Note  The examples that follow illustrate a very mon situation where an architecture worked and the  

           ideas of the architecture could be applied in the new context。 What does not work is the actual implementa

           tion of the original architecture。 You may be tempted to modify the working architecture so that it will work  

           with the new context。 As much as you would like to do that�察�don¨t。 Often�察�the abstractions you create will  

           plicate the architecture and make the resulting code more plex。 Your challenge is to know when to  

           generalize an architecture and when to keep the ideas of an architecture but create a new implementation�察 �

           and you¨ll learn this with experience。 



           Defining the Interfaces and Implementing the Shell 



           In �察�the data streams are split into two types of streams�此�binary and text。 The TextWriter  

           and TextReader types are used to read text´based data streams。 As was demonstrated with the  

           StringReader type�察�when dealing with text´based streams�察�certain assumptions can be made�察 �

           such as being able to find new lines in the stream。 With binary data streams�察�no such assump

           tions can be made。 Binary streams have their own format�察�which is known by only the program  

           doing the reading or writing。 

                The binary stream´based types can be used to process text data streams�察�but doing so would  

           require knowing the details of the data stream。 Remember that  gives you a text´handling  

           system that understands the different Unicode code pages。 A Unicode code page is a specific  

           translation map。 If you decide to manipulate the text streams using binary stream types�察�you  

           are telling  that you will manage the details of the Unicode code pages。 Of course�察�you  

           don¨t want to do that�察�and thus should never mix data streams。 So�察�for our sample application�察 �

           we need to design two different interfaces�此�one to stream from text to binary and one to stream  

           from binary to text。 



           *Note  For more information about Unicode and other text´related issues�察�see the MSDN ^International Text  

           Display ̄ section ��http��//msdn2。microsoft。/en´us/library/ms776131。aspx��。 



                The following is the binary´to´text data stream interface�察�Binary2Text。IBinary2TextProcessor。 



           Imports System。IO 



           Public Interface IBinary2TextProcessor 

               Sub Process��ByVal input As Stream�察�ByVal output As TextWriter�� 

           End Interface 


´´´´´´´´´´´´´´´´´´´´´´Page 293´´´´´´´´´´´´´´´´´´´´´´´

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