友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!
富士康小说网 返回本书目录 加入书签 我的书架 我的书签 TXT全本下载 『收藏到我的浏览器』

VB2008从入门到精通(PDF格式英文版)-第16部分

快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部! 如果本书没有阅读完,想下次继续接着阅读,可使用上方 "收藏到我的浏览器" 功能 和 "加入书签" 功能!



format of the number is hexadecimal (NumberStyles。HexNumber; from the System。Globalization  

namespace)。  



■Note   If you are wondering how 100 maps to 256 at the hex level; use the calculator that es with the  

Windows operating system。 Switch the calculator to scientific view (View  Scientific)。 Click the Hex radio  

button; enter the number 100; and then click the Dec radio button。  



     The enumeration NumberStyles has other values that can be used to parse numbers according  

to other rules。 For example; some rules handle the use of parentheses surrounding a number  

to indicate a negative value。 Other rules deal with whitespace。 Here is an example: 



Public Sub TestParseNegativeValue() 

    Dim value As Integer = Integer。Parse( 〃 (10) 〃; _ 

    NumberStyles。AllowParentheses Or _  

    NumberStyles。AllowLeadingWhite Or _ 

    NumberStyles。AllowTrailingWhite) 

End Sub 



     The number 〃  (10)  〃 in this example is plicated in that it has whitespace and paren

theses。 Attempting to parse the number using Parse() without using any of the  NumberStyles  

enumerated values will not work。 The enumeration AllowParentheses processes the parentheses;  

AllowLeadingWhite ignores the leading spaces; and AllowTrailingWhite ignores the trailing  

spaces。 Then; when the buffer has been processed; a value of –10 will be stored in the variable  

value。 

     Other NumberStyles enumerated values allow you to process decimal points for fractional  

numbers; positive or negative numbers; and so on。 This then raises the topic of processing  

numbers other than Integer。 Each of the base data types; such as Boolean; Byte; and Double;  

has associated Parse() and TryParse() methods。 Additionally; the method TryParse() can use  

the NumberStyles enumeration。 (See the MSDN documentation for details on the NumberStyles  

enumerated values。) 

     Parsing integer values is the same; regardless of the country。 Parsing double values or dates  

is not the same。 Consider the following example; which tries to parse a buffer that contains  

decimal values。 



Public Sub TestDoubleValue() 

    Dim value As Double = Double。Parse(〃1234。56〃) 



    value = Double。Parse(〃1;234。56〃) 

End Sub 



     In this example; both uses of the  Parse() method process the number 1234。56。 The first  

Parse() method is a simple parse; because it contains only a decimal point separating the  

whole number from the partial number。 The second Parse() method is more plicated in  


…………………………………………………………Page 94……………………………………………………………

72        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   



          that a ma is used to separate the thousands of the whole number。 In both cases; the  

           Parse() routines did not fail。  

                If you test this code; it’s possible that an exception will be generated。 In this case; the culture of  

          the application is to blame。 The numbers presented in the example are encoded using en…CA;  

          which is English…Canadian notation。 



          Working with Cultures 



          In ; culture information is made up using two identifiers: language and specialization。 As  

          I mentioned earlier; in Switzerland; there are four spoken languages; which means that there  

           are four different ways of expressing a date; time; and currency。 This does not mean that the  

           date is different for German speakers and French speakers。 The date format will be identical;  

          but the words (Maerz or Mars for the month March) will be different。 The words for the date are  

          the same in Austria; Switzerland; and Germany; but the format is not identical。 This means  

          multilanguage countries such as Canada (French and English) and Luxembourg (French and  

           German) need to be able to process multiple encodings; hence the need for the two identifiers。 

                To retrieve the current culture; use the following code: 



           Dim info As System。Globalization。CultureInfo = _ 

              System。Threading。Thread。CurrentThread。CurrentCulture 

           Console。WriteLine(〃Culture (〃 & info。EnglishName & 〃)〃) 



                The property Thread。CurrentThread。CurrentCulture retrieves the culture information  

           associated with the currently executing thread。 As a side note; it is possible to associate  

           different threads with different cultural information。 The property EnglishName generates an  

          English version of the culture information; which would appear similar to the following: 



           Culture (English (Canada)) 



                Consider the following number: 



           1;234 



                The number with an American or Canadian culture is one thousand two hundred thirty

          four; but with a German culture; it is one point two three four (for those who do not know about  

           German formatting; a ma is used as a decimal separator; and a period is used as a thousands  

           separator)。 One way to change the culture is with the dialog box shown earlier in Figure 3…12。 The  

           second way to change the culture is at a programmatic level; as in this code: 



           Imports System。Threading 

          Thread。CurrentThread。CurrentCulture = new CultureInfo(〃en…CA〃) 



                In the example; a new instance of CultureInfo is created with the culture information en…CA。 

                Next is an example that processes a double number encoded using German formatting rules: 



           Public Sub TestGermanParseNumber()  

              Thread。CurrentThread。CurrentCulture = new CultureInfo(〃de…DE〃) 

              Dim value As Double = Double。Parse(〃1;234〃) 

           End Sub 



                This example assigns the de…DE culture information to the currently executing thread。 Then  

          whenever any parsing routines are used; German from Germany is used as the basis for the  


…………………………………………………………Page 95……………………………………………………………

                                           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  73 



formatting rules。 Changing the culture information does not affect the formatting rules of the  

programming language。  

     It is also possible to parse dates and times using the Parse() and TryParse() routines; as  

demonstrated by the following examples: 



Public Sub TestGermanParseDate() 

    Dim datetime As Date = Date。Parse(〃May 10; 2008〃) 

    If 5 = datetime。Month Then 

         Console。WriteLine( 〃correct〃) 

    End If 

    Thread。CurrentThread。CurrentCulture = new CultureInfo(〃de…DE〃) 

    datetime = Date。Parse(〃10 Mai; 2008〃) 

    If 5 = datetime。Month Then 

         Console。WriteLine( 〃correct〃) 

    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 U CT U R E S;   DE CI SI ON S;   A N D  
返回目录 上一页 下一页 回到顶部 11 11
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!