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

第20章

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

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

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






     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 number of  

           spaces and then subtract 1。 In the case of the Integer type; there are 32 spaces。 Before we calculate  

           the biggest number Integer can store; though; we need to consider negative numbers。 The  

           upper limit of Integer isn’t actually 4;294;967;295 (the result of 232 – 1); because  Integer also  



           stores negative numbers。 In other words; it can save a negative whole number; such as –2。  

                 The puter uses a trick in that the first space of the number is reserved for the sign (plus  

           or minus) of the number。 In the case of Integer; that means there are only 31 spaces for  

           numbers; so the largest number that can be represented is 2;147;483;647; and the smallest is  

           –2;147;483;648。 Going back to our addition example; this fact means that when the result of our  

           addition is 4 billion; which in binary requires 32 spaces; Integer does not have the space to store it。 

                 The  environment includes the numeric data types listed in Table 2…1; which have  

           varying sizes and storage capabilities。 The following terminology is used to describe numeric  

           data types: 



                o  A bit is a space of storage; and 8 bits make a  byte。 



                o  Integers are whole numbers。 



                o  Floating…point types are fractional numbers。 



                o  Signed means one space in the number is reserved for the plus or negative sign。 



           Table 2…1。   Numeric Data Types 



           Type                Description 



           Byte                Unsigned 8…bit integer; the smallest value is 0; and the largest value is 255 



           SByte               Signed 8…bit integer; the smallest value is –128; and the largest value is 127 



           UShort              Unsigned 16…bit integer; the smallest value is 0; and the largest value is 65535 



           Short               Signed 16…bit integer; the smallest value is –32768; and the largest value is 32767 



           UInteger            Unsigned 32…bit integer; the smallest value is 0; and the largest value is  

                               4294967295 



           Integer             Signed 32…bit integer; the smallest value is –2147483648; and the largest value is  

                               2147483647 



           ULong               Unsigned 64…bit integer; the smallest value is 0; and the largest value is  

                               18446744073709551615 



           Long                Signed 64…bit integer; the smallest value is –9223372036854775808; and the  

                               largest value is 9223372036854775807 



           Single              32…bit floating…point number; the smallest value is –3。4x1038; and the largest  

                               value is 3。4x1038; with a precision of 7 digits 



           Double              64…bit floating…point number; the smallest value is –1。7x10308; and the largest  

                               value is 1。7x10308; with 15 to 16 digits of precision 



           Decimal             Special 128…bit data type; the smallest value is 1。0x10–28; and the largest value is  

                               1。0x1028; with at least 28 significant digits of precisiona 



           a  The Decimal type is often used for financial data because sometimes a calculation will result in one penny  



             less than the correct result (for example; 14。9999; instead of 15。00) due to rounding errors。 


…………………………………………………………Page 67……………………………………………………………

                                    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  45 



     With so many variations of number types available; you may be wondering which ones to  

use and when。 The quick answer is that it depends on your needs。 When performing scientific  

calculations; you probably need to use a Double or Single。 If you are calculating mortgages; you  

probably need to use a Decimal。 And if you are performing set calculations; you probably should  

use an Integer or a  Long。 It all depends on how accurate you want to be; or how much numeric  

precision you want。 

     Numeric precision is an important topic and should never be dealt with lightly。 Consider  

the following example: every country takes a census of its people; and when the census is  

piled; we learn some interesting facts。 For example; in Canada; 31% of people will divorce。  

Canada has a population clock that says every minute and 32 seconds; someone is born。 At the  

time of this writing; the population was 32;789;736。 Thus; at the time of this writing; 10;164;818  

people will divorce。 Think a bit about what I just wrote。 I said that there is a direct relationship  

of people who will divorce to the number of births in Canada (31%; in fact)。 You should be  

amazed that the births and divorces are timed to the point where 10;164;818—not 10;164;819  

nor 10;164;820—people will divorce。 Of course; I’m being cynical and just trying to make the  

point that numbers are just that: numbers that you round off。  

     I can’t say 10;164;818 people will divorce; because I can’t be that accurate without performing  

an actual count。 I could probably say 10;164;818 plus or minus 100;000 will divorce。 Using the  

plus or minus; the range is 10;064;818 to 10;264;818; or roughly speaking; 10。2 million people。  

The number 10。2 million is what a newsperson would report; what literature would say; and  

what most people would use in conversation。 So; if I add 10。2 million and 1;000; can I say that  

the total is 10;201;000? The 10。2 is a roundoff to the nearest tenth of a million; and adding a  

thousand means adding a number that is less than the roundoff。 The answer is that I cannot  

add 1;000 to 10。2; because the 1;000 is not significant w

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

你可能喜欢的