小牛电子书 > 其他电子书 > VB2008从入门到精通(PDF格式英文版) >

第30章

VB2008从入门到精通(PDF格式英文版)-第30章

小说: VB2008从入门到精通(PDF格式英文版) 字数: 每页3500字

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




    End If 

     

End Sub 



     Notice how the first Date。Parse() processed English…Canadian formatted text and knew  

that the identifier May equaled the fifth month of the year。 For the second Date。Parse() method  

call; the culture was changed to German; and it was possible to process 10 Mai; 2008。 In both  

cases; processing the buffer posed no major problems; as long as you knew that the buffer was  

a German or English…Canadian date。 Where things can go awry is when you have a German  

date and an English culture。 

     Converting a data type to a buffer is relatively easy because the ToString() methods have  

been implemented to generate the desired output。 Consider the following example; which  

generates a buffer from an integer value: 



Public Sub TestGenerateString()  

    Dim iValue As Integer = 123 

    Dim buffer As String = iValue。ToString() 

    If buffer = 〃123〃 Then 

         Console。WriteLine( 〃correct〃) 

    End If 

End Sub 



     In the example; the value 123 has been assigned to  iValue; which is of type Integer; and  

then its ToString() method is called; which generates a buffer that contains  〃123〃。 The same  

thing can be done to a Double value; as in this example: 



Dim number As Double = 123。5678 

Dim buffer As String = number。ToString(〃0。00〃) 



     Here; the number 123。5678 is converted to a buffer using the ToString() method; but  

ToString() has a parameter; which is a formatting instruction that indicates how the double  

number should be generated as a buffer。 The desired result is a buffer with a maximum of two  

digits after the decimal point。 Because the third digit after the decimal is a 7; the value is  

rounded up; resulting in the buffer 123。57。 

     Let’s see an example where the culture information also applies to generating a buffer。  

Here; a Double value is generated in the format of the culture: 


…………………………………………………………Page 96……………………………………………………………

74        CH AP T E R   3   ■    L E A R N IN G   AB OU T   ST R I N G   M A N I P U L AT IO N S   



          Public Sub TestGenerateGermanNumber()  

              Dim number As Double = 123。5678 

              Thread。CurrentThread。CurrentCulture = _ 

                  new CultureInfo(〃de…DE〃) 

              Dim buffer As String = number。ToString(〃0。00〃) 

              If buffer = 〃123;57〃 Then 

                   Console。WriteLine( 〃correct〃) 

              End If 

          End Sub 



               As in the previous examples; the CurrentCulture property is assigned the desired culture。  

          Then when the Double variable number has its ToString() method called; the buffer  〃123;57〃  

          is generated。 



          The Important Stuff to Remember 



          In this chapter; you learned about strings and writing code。 Here are the keys points to  

          remember: 



               o  Writing tests is an important part of your development practice。 A test is not just a mech

                  anism to catch errors; but also a mechanism used to understand the dynamics of your code。 



               o  The String type is a special reference type that has many methods and properties。 You  

                  are advised to look at the MSDN documentation to see what a string can do。 



               o  IntelliSense and the MSDN documentation are your best bets when you want to find out  

                  about specific methods; properties; or types。 Books and web sites such as Code Project  

                  are good resources to help you understand concepts。 



               o  All variables and types are based on the object type。 



               o  When writing code; you need to define responsibilities and contexts。 Don’t fix bugs or  

                 write code using knee…jerk reactions。 



               o  All strings are based on Unicode。 Each Unicode character is 16 bits wide。 



               o  When translating buffers; you need to deal with the translation of text and the translation of  

                  numbers and dates。 



               o   includes sophisticated technology to help you translate numbers and dates using a  

                  bination of language and culture information。  


…………………………………………………………Page 97……………………………………………………………

                                             CH AP T E R   3   ■    L E AR N IN G   AB O U T   ST R I N G   M A N I PU L A TI O N S  75 



Some Things for You to Do 



The following are some exercises that relate to what you’ve learned in this chapter。 



     1。  Finish the application to translate from one language to another language; allowing the  

         user to choose which direction the translation takes。 



     2。  Extend the  LanguageTranslator ponent to be able to translate the words  au revoir  

         and auf wiedersehen to good bye。 



     3。  You can bine strings by using the plus sign; but doing many additions will slow  

         down your code。 Use the StringBuilder class to concatenate two buffers together。 Hint:  

         you want to convert the code String c = a + b; and make a and b use the StringBuilder  

         class。 The result of the StringBuilder is assigned to the variable c。 



     4。  Create a test that demonstrates what happens when a number value is added to a string  

         value。 Write the appropriate tests to verify your conclusion。 



     5。  Extend the  LanguageTranslator ponent to include methods to translate English  

         numbers into German numbers。 



     6。  Extend the LanguageTranslator ponent to include methods to translate dates from  

         American or Canadian dates into German dates。 Note that the added wrinkle is that you  

         could input an American or Canadian date。 



     7。  Implement the Windows application that calls the  LanguageTranslator ponent。 


…………………………………………………………Page 98……………………………………………………………


…………………………………………………………Page 99……………………………………………………………

C  H  A  P  T  E  R     4 



■ ■ ■ 



Learning About Data Structures;  

 Decisions; and Loops 



When you are creating applications; the source code will need to make decisions; such as  

should you open the file or save the file? And if you open the file; what kind of iterative code is  

going to read the contents of the file? These sorts of questions are answered by employing data  

structures; decisions; and loops。  

     The easiest way to demonstrate how to make a decision is to write a miniature artificial  

intelligence (AI) system。 The AI system will be extremely primitive; but AI is interesting because  

it makes extensive use of decision and loop constructs。 The AI system iterates and makes deci

sions based on data defined in a neat and orderly custom data structure。  

     Using the example of creating an algorithm; the following topics will be covered in this  

chapter: 



    o  Data structures; including custom types 



    o  The restrictions of value types 



    o  Algorithm design 



    o  Class constructors; which allow you to initialize an object 



    o  The For loop; which allows you to iterate over sets of data 



    o  The If statement; which allows you to execute specific code based on logic 



Understanding the Depth…First Search Algorithm 



AI involves searching for data; and a core algorithm of AI is searching。 The search algorithm  

that we will develop for this chapter’s example is a depth…first search system。 AI has other types  

of searches; such as an A* or a breadth…first search; but they are all based on the same idea as  

the depth…search algorithm。 This idea is searching for information that is arranged in a tree  

structure。 



                                                                                                           77 


…………………………………………………………Page 100……………………………………………………………

78         CH AP T E R   4   ■    L E A R N IN G   AB OU T   D AT A  S TR

返回目录 上一页 下一页 回到顶部 2 2

你可能喜欢的