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

第25章

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

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

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




          has wineglasses from Pier 1。 If you use the Equals() method to pare the boxes; it will return  

           False; because the box content details are not identical。 The difference lies in how the glasses  


…………………………………………………………Page 81……………………………………………………………

                                                 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  59 



are described。 In contrast; calling GetHashCode() will indicate that the contents of the boxes are  

identical。 This is because GetHashCode() is doing a quick contents identification。 

      The difference between the  Equals() and GetHashCode() methods is perspective。 In the  

example; from the perspective of the moving pany; the boxes are identical because the  

moving pany does not care whether the wineglasses are from IKEA or Pier 1; it will not  

distinguish wineglasses from different panies。 

      The fact that GetHashCode() can return identical numbers for what would seem dissimilar  

object contents can confuse developers。 The way to understand GetHashCode() is that; rather  

than being useful to verify equality; it helps you verify  inequality。 If two objects return dissim

ilar hash code values; then you know the contents are not identical。 The intent of a hash code  

is to generate a quick fingerprint of the contents of an object。 It is not pletely reliable; but  

works for the most part。  



                                   WHEN INTELLISENSE IS NOT ENOUGH  



   IntelliSense is very good and will even display ments that explain what the method does (as illustrated in  

   Figure 3…2)。 Another place to find answers is the Microsoft documentation itself; which you can access by  

   selecting Help  Index。 You can use the Look For box to search for a specific type。 For example; if you type  

   “String class” in the Look For box; you will see details of the String class; which you can then filter using the  

   links at the top of the page。 

         The Microsoft documentation is part of the Microsoft Developer Network (MSDN) ( http:// 

   msdn。microsoft。)。 The MSDN web site contains documentation that helps you figure out the  stan

   dard software development kit ( SDK) application programming interface (API)。 There are literally thou

   sands of types; with an explosion of methods and properties。 You will definitely not use all of them in a single  

   application; but you will always use the  SDK。 

         In most cases; MSDN will be enough to help you figure out when you don’t know about a specific type。 

    If you want to learn more about concepts; you can surf to a web site such as Code Project (http:// 

   codeproject。)。 Code Project contains plenty of examples for almost every development topic that  

   suits your fancy。 



The Problem: Character…by…Character parison 



Let’s get back to the bug of the whitespace。 The method that caused problems was pareTo()。  

Looking at the MSDN documentation; you see the following definition for this method (found  

by scrolling down the String class page and clicking the pareTo link): 



      pares this instance with a specified Object。 



      This definition does not tell you much; so you will need to go back to the String page and  

click another method name。 Click the pare link; and then click the pare(String; String)  

link。 In the explanation of the pare() method; you find the following text: 



      The parison terminates when an inequality is discovered or both strings have been 

      pared。 However; if the two strings pare equal to the end of one string; and the 

      other  string  has  characters  remaining;  then  the  string  with  remaining  characters  is 

      considered greater。 The return value is the result of the last parison performed。 


…………………………………………………………Page 82……………………………………………………………

60         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   



           ■Note  Looking up the meaning of a method is not a long process; even though it may seem like that from  

           the description in this section。 With experience; you don’t even notice the extra clicks。  



                The pareTo() method failed because of the character…by…character parison; which  

           is illustrated in Figure 3…7。 



           Figure 3…7。 How pareTo() fails to pare strings that appear identical but have extra  

           characters 



                 Strings are stored in buffers; with one space in the buffer allocated to one character。 As you  

           can see from Figure 3…7; whitespace takes up one space in the buffer。 We can take advantage of  

           this sometimes; as you’ll see in the next section。 

                 Now that you know what the problem is; the next step is to find a solution。 



           Solving the Extra Whitespace Problem 



           You can solve the whitespace problem in multiple ways。 Which way you use depends on your  

           needs。 Let’s look at several solutions and see which works best for our translation program。 



           Trimming the Whitespace 



           The first solution we’ll look at is to trim the whitespace using a method intended for that purpose。  

           The whitespace problem is not unique and is well known。 The String type has a method that  

           can be used to remove; or trim; the whitespace from a buffer。 You can remove whitespace at  

           the beginning; end; or both sides of the buffer。  


…………………………………………………………Page 83……………………………………………………………

                                              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  61 



     As tempting as it is to change the original implementation of  TranslateHello(); don’t do  

that; because you might inadvertently break something that is working just fine。 When you  

develop code; you have multiple possible ways to solve a problem。 If you start messing around  

with the original source code; by the time you reach the third or fourth solution; the code might  

be a plete mess。 Your fixes might make things worse; and trying to backtrack in your source  

code bees very difficult。  



■Note  To manage your source code; you should use version control。 However; even with version control;  

when you delete past attempts; ideas get lost。 Thus; while your source code will be clean; you might forget  

something you did three or four hours ago。 Trust me; this happens; because developing source code is an  

intensive thought process。 



     The solution is to create a shim that calls the TranslateHello() method。 The shim is used  

to fix the bug。 The following shim code is a temporary solution: 



    Public Function TrimmingWhitespace(ByVal buffer As String) As String 

        Return Translator。TranslateHello(buffer。Trim()) 

    End Function 



      TrimmingWhitespace() is a method that trims whitespace from the string to be translated。  

buffer。Trim is new functionality that preprocesses the buffer。 Finally; we call the original  

method; TranslateHello(); to perform the translation。 

      Of course; we need to test the new method to see if it trims the string to be translated。 The  

following is the corresponding test code。 



Dim verifyValue As String = TrimmingWhitespace(〃  allo〃) 

If verifyValue。pareTo(〃hallo〃)  0 Then 

    Console。WriteLine(〃Extra white spaces allo to hallo test failed〃) 

End If 



     The test calls the work…in…progress method; TrimmingWhitespace(); to see if everything  

works。 The verification code does not change。 

      So that you get predictable results; remember to call the shim and not the original method。  

If you run the test code; you’ll see that the shim works; and thus we have a solution。 



Finding a Substring 



Another solution to the whitespace problem is to find a specific substring in a buffer。 Think of  

it as a search solution; where a me

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

你可能喜欢的