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

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

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


           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 billion。 


…………………………………………………………Page 63……………………………………………………………

                                     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  41 



Figure 2…13。 How a puter counts to 7 



      The bad part of an odometer is that you don’t know when the odometer has been wrapped  

around。 You could end up with a car whose odometer read 100;000 miles/kilometers; but actu

ally had driven 1。1 million。 Fortunately;  knows when the number type has been wrapped  

around。 The technical jargon for this situation is overflow or  underflow。 Overflow is when the  

odometer wraps over in the positive direction (900;000 to 1。1 million); and underflow is when  

the odometer wraps around in the negative direction (0 to –100;000)。 Detection of either situa

tion is activated as a project property。 Follow these steps to deactivate overflow/underflow  

detection for the Calculator class library: 



      1。 Right…click the Calculator project in the Calculator solution and select Properties。 



      2。 Click pile; and then click Advanced pile Options。 



      3。 Check “Remove integer overflow tests” to remove the test for overflow situations。 



      4。 Click OK to finish。 



      Rerun the test console application; and you will get an exception indicating that an over

flow has occurred。 



Error found(…294967296) should have been (4000000000) 



      Having an overflow situation is a problem; and the fact that  can catch it is a good  

thing。 But at the end of the day; our preferred solution is to be able to add 2 billion plus 2 billion。  

After all; Bill Gates would probably prefer having 4 billion in his bank account instead of the  

calculated minus value or an error indicating that the bank cannot accept his 4 billion。 


…………………………………………………………Page 64……………………………………………………………

42        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  



           Understanding Numeric and Value Data Types 



          A  data type is a way of describing a piece of data using a metadescription。 For example; if you  

           have a Double type; you know you will have a double number。 Many data types are available:  

           Integer; Long; Short; Single; Double; String; Enum; Structure; and so on。 You can even define your  

           own data types。 Data types are the heart of the CLR and a type…safe programming environment。  



           Understanding Value and Reference Types 



           The CLR supports two ways of representing data: value and reference。 The major difference  

           between a value and reference type is how the information associated with the type is stored。  

           The problem with value and reference types is that they are a technical invention and can be  

           confusing。  

                When the CLR runs an application; a thread is executing the mon Language Infra

           structure (CLI)。 Think of a thread as you wandering around a mall buying things。 You are an  

           individual and can buy things independently of other people。 The store will have many people  

           looking around and buying different things。 Likewise; a puter has many threads doing  

           many different independent things。 When you look around in a store; you might bump into  

           people and cause them to drop things。 While the CLR tries to avoid such problems; if you try  

           hard enough in your code; you can cause other threads to “drop” things。 

                When it executes; a thread has a local memory pool called a stack; which is akin to you  

           carrying a wallet that contains cash and credit cards。 You carry the wallet with you from store  

           to store; like a thread carries a stack when calling one method to another。 When you enter the  

           store and want to purchase something; you have two major ways of paying for the item: with  

           cash or by using a credit/debit card。 However; with a credit/debit card; you can’t pay immedi

           ately。 You need a machine that calls a server to verify that your piece of plastic has enough money  

           to pay for the item。 Paying with cash is much faster than paying with a credit card; because you  

           don’t need to talk to a remote puter。 

                Now suppose you and your spouse want to pay for the item。 You could use the same credit  

           card account; but you have unique credit cards。 But you cannot do the same thing with cash。  

           If you have a 10 bill; your spouse cannot share the 10 with you。 Your spouse would need a  

           second 10 bill; and together you would have 20。 

                The cash and credit card payment methods are analogous to value and reference types。  

           Cash is a value type; and the credit card is a reference type。 When the CLR executes; the code  

           dragged from one method call to another method call is the stack that contains a number of  

           value type variables。 Value types are stored directly on the stack like cash。 Reference types are  

           stored as pointers to memory on the stack; just like a credit/debit card points to cash some

           where else。 The reference pointer points to a piece of memory called the  heap。 These concepts  

           are illustrated in Figure 2…14。 

                With value types; when one value type is assigned to another; the contents are copied。 If  

          you modify one of the copies; the original will not be altered。 In contrast; when you change the  

           value of a reference type; the values of all the pointers to that reference type change。 Going  

           back to the credit card and cash example; if you have 10 and so does your spouse; then when  

          you spend 8; it does not affect the 10 that your spouse has; as befits the value type model。  

           However; if you and your spouse have 10 available with your credit card and you spend 8;  

           only 2 remain; as you would expect with a reference type。 


…………………………………………………………Page 65……………………………………………………………

                                  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  43 



                                                             The variable total is a value type 

                                                                 and stored on the stack 



   

  

                                                                                        Stack 

                                                                             1:total = 3 

     
   

           ! 〃 



    #  % & ' 

   
  ()*   〃   + &* 

    ; # 

    ;                            Method call 



                                                   Stack 

       

                                                   empty 

                                                There are no parameters 

   
  … 。/                                               and no local variables; 

    ;                                                             thus the stack is empty 



                                   Each called function has a stack 

   ;   

                                      that contains the function 

                                  arguments and variables declared 

                                            in the function 



Figure 2…14。 Stacks that are created and the interaction with the heap during CLR execution 



     There are times when you use value types and times when you use reference types; just as  

there are times when you pay for things using cash and times when you use a credit card。 Typi

cally though; you use credit cards when you want to pay for expensive things; because you  

don’t want to carry around large amounts of cash。 This applies to value and reference types; in  

that you don’t want to keep large footprint value types on the stack。 

     By knowing the difference between the stack and heap; you automatically know the differ

ence between a value type and a reference type; as they are directly related。 Value types are  

stored on the stack; and the contents of reference types are stored on the heap。 



Understanding the CLR Numeric Types 



The CLR has two major types of numbers: whole numbers and fractional numbers。 Both of  

these number types are value…based data types; as explained in the previous section。 The Add()  

method used the type Integer; which is a whole number–based value type。 As you saw; whole  

numbers have upper limits; which are set by the space available。 

     Consider the following number: 



123456 



     This number takes six spaces of room。 For illustrative purposes; imagine that the page you  

are reading allows only six spaces of room for numerals。 Based on that information; the largest  

number that can be written on this page is 999;999; and the smallest is 0。 In a similar manner;  

specific number types force the CLR to impose restrictions on how many spaces can be used to  

represent a number。 Each space is a 1 or a 0; allowing the CLR to represent numbers in binary  

notation。 


…………………………………………………………Page 66……………………………………………………………

44         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  



                 puters may use binary notations; but humans work better with decimals; so to calcu

           late the largest possible number a data type can store; you use 2 to the power of the 
返回目录 上一页 下一页 回到顶部 0 0
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!