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

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

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





´´´´´´´´´´´´´´´´´´´´´´Page 56´´´´´´´´´´´´´´´´´´´´´´´

34         CH AP T E R   2   *    L E A R N IN G   AB OU T   。 N E T  N U M B E R   A N D   V A L U E   T Y P E S  



           Function declaration implies that the caller expects to get a value back from the method。 The  

           data type of the returned value is specified by the As Integer keywords!this particular func

           tion will return an Integer value。 ��If you want to define a method that does not return a value�察 �

           use Sub rather than  Function。�� 

                 Methods and parameters must be associated with a type�察�as Visual Basic is a type´safe  

           programming language。  Type´safe means that when you write code�察�you know what you are  

           manipulating。 

                 Suppose that you are writing code and are confronted with the numbers 1�察�1。0�察�and ;1。0;。  

           To you�察�these three numbers are identical。 But in the context of the source code�察�they are not  

           identical。 The 1 is an integer�察�the  1。0 is a double�察�and the  ;1。0; is a string。 When you want to  

           add�察�subtract�察�or otherwise manipulate pieces of data�察�they should be the same types�察�other

           wise�察�you might run into consistency errors。 Type´safe programming languages help avoid  

           such problems。 The  number types are discussed in more detail in the ^Understanding the  

           CLR Numeric Types ̄ section later in this chapter。 

                 The declaration of Add�┌� says that we need to pass in two integer´based numeric values�察 �

           and the method returns an integer´based numeric value。 The bination of parameters and  

           a return type is a method signature。 The method signature bees important when another  

           piece of code calls the Add�┌� method。 The other piece of code must use the same types as the  

           declaration。 Figure 2´7 shows a piece of code that calls the Add�┌� method�察�which we¨ll do from  

           another application in the next section。 



                                                                     Namespace           Class             Method 



                 Dim total As Integer = Calculator。Operations。Add��1�察�2�� 



                         total is an integer declared                                           1 and 2 are integer values�察�

                        variable that stores the result                                          which represent the two 

                               of the addition                                               numbers that are added together 



           Figure 2´7。  The Add�┌� method is called by referencing the namespace and class containing the  

           method。 A period is used to separate the identifiers。 



                 The caller must do two things�此 �



                o  Reference the correct bination of namespace�察�class�察�and method identifiers。 



                o  Pass the correct types into the method�察�as specified by the method signature。 



                 In the example�察�the addition of 1 and 2 results in 3�察�and therefore the variable total should  

           contain the value 3 ��the equal sign assigns the value returned from the method to the variable  

           on its left��。 I say ^should contain the value�察院�because when writing code�察�you are not always  

           sure。 Sometimes the code you write will be wrong because you overlooked something or forgot  

           to reference something。  

                 Look at the calling code�察�and ask yourself if you are guaranteed that calling Add�┌� with  1  

           and 2 will result in 3。 The answer is that�察�as a caller�察�you cannot be 100�ァ�sure that the total variable  

           will contain 3。 Just because a box has the label ^Dishes ̄ does not necessarily mean that dishes  


´´´´´´´´´´´´´´´´´´´´´´Page 57´´´´´´´´´´´´´´´´´´´´´´´

                                       CH A PT E R   2   *    L E A R N I N G   A B OU T   。 N E T  N U M B E R   AN D   V A L U E   T Y P E S  35 



are in the box。 You think you know the contents�察�but you cannot be 100�ァ�sure until you open  

the box。 Likewise�察�in code�察�you need to look at how the Add�┌� method is implemented to be sure  

of the contents of the total variable。  

      In a production coding session�察�looking at the implementation code to verify it is doing  

what you expect is not a feasible solution�察�because that would take too much time and be  

pletely unreliable。 The only real solution is to write test code。  



Writing Code to Test the Add�┌� Method 



Test code is caller code that passes parameters with targeted values and expects a targeted  

answer。 If the caller does not get the targeted answer�察�then the implementation of the tested  

method is wrong。 Figure 2´8 shows sample caller code that tests the Add�┌� operation ��we¨ll add  

this to a project next��。  



                                                            Targeted caller code that adds 

                                                            1 and 2 and assigns the result 

                                                                 to the variable total 



                    Dim total As Integer = Operations。Add��1�察�2�� 



                    If ��total  3�� Then 

                     Console。WriteLine�─�Oops 1 add 2 does not equal 3;�� 

                    End If 



   Targeted testing              If targeted testing fails�察�the 

    of the variable              text ^Oops´ ̄ is generated�察�

   total�察�paring it               indicating an error 

     to the value 3 



Figure 2´8。  Testing the Add�┌� method 



      The calling code of the test bears an uncanny resemblance to the code you saw in the previous  

section。 The difference is that the test code uses targeted variables and values�察�whereas the  

other code could contain any variables and values。 Another requirement of test code is to verify  

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