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

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

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






Imports Calculator 



Module Module1 

    Public Sub TestSimpleAddition�┌� 

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



        If ��total  3�� Then 

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

        End If 

    End Sub 



    Sub Main�┌� 

        TestSimpleAddition�┌� 

        Console。ReadKey�┌� 

    End Sub 



End Module 


´´´´´´´´´´´´´´´´´´´´´´Page 60´´´´´´´´´´´´´´´´´´´´´´´

38         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  



                 Press Ctrl��F5 to test the calculation。 When executed�察�the testing console application calls  

           the test method TestSimpleAddition�┌��察�which calls and verifies the functionality of the Calculator  

            class library。 If everything works�察�the message will not appear。 



           *Note  Recall that the Main�┌� method is where a project begins its operation。 To make an application do  

           something�察�you must add code to its Main�┌� method。 



                 To see that the test also fails�察�change the Add�┌� method as follows�此�



               Public Shared Function Add��ByVal number1 As Integer�察�ByVal number2 As _ 

            Integer�� As Integer 

                   Return number1 * number2 

               End Function 



                 Now rerun the program�察�and you¨ll see the failure message。 



                                                     THE DEVELOPMENT CYCLE 



              So far in this chapter�察�we¨ve dealt with three pieces of code�此 �



                 o   The code segment that implements the Add�┌� method performs the calculation operation。 



                 o   The code that represents a caller�察�which could be either the Windows application or the console appli

                     cation�察�is considered production code。 



                 o   The code that contains the production code with some verification routines represents testing code。 The  

                     testing code is important because�察�if the ponent implementation is altered�察�you need to rerun only  

                     the testing code to make sure everything works as it should。 



                    These three pieces of code have demonstrated a plete development cycle。 



           Testing the Addition of Two Very Large Numbers 



           The code and projects have been organized�察�but we¨re missing some tests。 The current test  

            adds two simple numbers。 Another test could be to add two really big numbers�察�such as 2 billion  

            and 2 billion。  

                 The code to test the addition of two really big numbers is shown in Figure 2´11。 Add this to  

           Module1。vb in the TestCalculator project。 


´´´´´´´´´´´´´´´´´´´´´´Page 61´´´´´´´´´´´´´´´´´´´´´´´

                                     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  39 



                          Addition result is 

                         stored in a variable 



                                                                                2 billion added to 2 billion 



  Public Sub TestReallyBigNumbers�┌� 

      Dim total As Integer = Operations。Add��2000000000�察�2000000000�� 



      If ��total  4000000000�� Then 

          Console。WriteLine�─�Error found�──�& total & _ 

                                 ;�� should have been ��4000000000��;�� 

      End If 

  End Sub 



                           Verification of addition is 4 billion    If there is an error�察�a string is built 

                                                                     using concatenation techniques 



Figure 2´11。  Testing the addition of two large numbers 



      The test to add two very large numbers is identical to the test to add two simple numbers�察 �

except for the numbers themselves。 The error message is handled a bit differently than in the  

previous code�察�in that it is built using concatenation techniques。 In the example�察�a string is  

concatenated with an integer with a string。 Visual Basic will automatically convert the integer  

into a string。 

      Often�察�you will write tests where the only real difference is the data itself�察�not the ramifica

tions of the data。 Do you think of adding two very large numbers ��such as 2 billion plus 2 billion��  

and two smaller numbers ��such as 2 plus 2�� as different�拭�No�察�because to humans�察�the major  

difference between 2 billion and 2 is a bunch of zeros�察�the result is either 4 billion or 4�察�which  

seems very trivial。 Yet with respect to a puter�察�4 billion is very different from 4�察�as you¨ll  

soon see。 

      Before you run the test�察�you need to add a call to it in the Main�┌� method�此�



    Sub Main�┌� 

        TestSimpleAddition�┌� 

        TestReallyBigNumbers�┌� 

        Console。ReadKey�┌� 

    End Sub 



      Now run the very large number test。 You will see the output shown in Figure 2´12。 

     What happened�拭�The problem has to do with the addition of very large numbers that cause  

an overflow�察�which is a puter construct。 


´´´´´´´´´´´´´´´´´´´´´´Page 62´´´´´´´´´´´´´´´´´´´´´´´

40        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  



           Figure 2´12。  Visual Basic Express highlighting the overflow error 



           Understanding Problems with Numeric Numbers 



           What you understand as numbers and what the puter understands as numbers are two  

           pletely different things。 As a child�察�you learn to count starting at 1 and ending at 100�察�which  

           you thought was a gargantuan number。 As you grew up�察�you learned about zero and numbers  

           less than zero。 Later�察�you advanced to studying fractions and decimal values。 

                Throughout all of this learning about numbers�察�you considered the number 1 and the  

           number 1。5 to be the same kind of thing!that is�察�numbers。 Yet to the puter�察�they are not  

           the same type of values。 

                The reason numbers are different to puters has to do with puter efficiency and  

           how the puter stores numbers。 For those who have learned the decimal system with place

           holders�察�you start counting at 0�察�continue to 9�察�and then the next number is a decimal increment  

           called 10。 The decimal system is originally accredited to the Babylonians�察�but they used a decimal  

           system that was sexagesimal ��60 unique identifiers versus our decimal 10 unique identifiers��。  

           The puter has a similar counting scheme�察�except it is binary with only two unique identi

           fiers�此�1 and 0。 puters have two unique identifiers because they signify two unique states�此 �

           on and off。 At the heart of a puter is a transistor that has the ability to distinguish between  

           on and off�察�there are no ^sort of ̄ on and ^sort of ̄ off states。  

                Figure 2´13 shows an example of how a puter would count to 7 using the binary system。 

                In theory�察�you could count until you die。 You have that ability because you are human and  

           have no limits on how high you can count。 A puter does have limits�察�such as random´access  

           memory ��RAM���察�hard disk�察�and so on。 Specific numeric data types on a puter also have  

           limits on how high they can count。 For example�察�the Integer data type that we have been using  

           can count to only a specific number and can store only whole numbers。  

                Think of the different numeric data types as odometers。 Most car odometers have upper  

           limits of 1 million miles/kilometers。 Imagine adding 900��000 and 200��000 using an odometer。  

           The result would be 100��000�察�and not the 1。1 million that you expected。 This is exactly the  

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