Section 7.11. Value Types and Reference Types


7.11. Value Types and Reference Types

As we discussed briefly in Chapter 4, all Visual Basic types can be categorized as either value types or reference types. A variable of a value type contains data of that type. Normally, value types are used for a single piece of data, such as an Integer or a Double value. By contrast, a variable of a reference type contains the address of the location in memory where the data referred to by that variable is stored. The actual object to which the variable refers can contain many individual pieces of data. Reference types are known as classes and are discussed in more detail in Chapters 911.

Both value types and reference types include primitive types and types that you can create. The primitive value types include the integral types (Byte, SByte, Short, UShort, Integer, UInteger, Long and ULong), the floating-point types (Single and Double) and types Boolean, Date, Decimal and Char. There is only one primitive reference typeString. Reference types can be created by declaring classes and modules, as well as interfaces and delegates, constructs that we will discuss in Chapters 11 and 13, respectively. The primitive reference type, String, actually represents the String class in the FCL. Value types can be created by declaring structures and enumerations. Structures, like classes, define types that contain members such as methods and instance variables. Enumerations may only contain sets of integral values. The primitive value types are defined using structures. Most of these structures have the same name as the Visual Basic primitive type (type Double is defined by structure Double), but others do not (type Integer is defined by structure Int32). These structures are part of the FCL, which is used by several languagesas a result, the naming of types in the various .NET languages may not always be identical to their underlying classes and structures in the FCL. User-defined types are discussed in greater detail in Chapter 9, Classes and Objects: A Deeper Look; Chapter 10, Object-Oriented Programming: Inheritance and Chapter 16, Strings, Characters and Regular Expressions. Figure 7.8 lists the primitive types, which form the building blocks for more complicated types, such as the GradeBook type we have defined in several examples in this book.

Figure 7.8. Primitive types.

Type

Size in bits

Values

Standard

Name of FCL class or structure

Boolean

16

true or False

 

Boolean

Byte

8

0 to 255

 

Byte

SByte

8

-128 to 127

 

SByte

Char

16

One Unicode character

(Unicode character set)

Char

Date

64

1 January 0001 to 31 December 9999 12:00:00 AM to 11:59:59 PM

 

DateTime

Decimal

128

1.0E-28 to 7.9E+28

 

Decimal

Double

64

±5.0E324 to ±1.7E+308

(IEEE 754 floating point)

Double

Integer

32

2,147,483,648 to 2,147,483,647

 

Int32

Long

64

9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

 

Int64

Short

16

32,768 to 32,767

 

Int16

Single

32

±1.5E-45 to ±3.4E+38

(IEEE 754 floating point)

Single

String

 

0 to ~2000000000 Unicode characters

(Unicode character set)

String

UInteger

32

0 to 4,294,967,295

 

UInt32

ULong

64

0 to 18,446,744,073,709,551,615

 

UInt64

UShort

16

0 to 65,535

 

UInt16


Each value type in the table is accompanied by its size in bits (there are 8 bits to a byte) and its range of values. To promote portability, Microsoft chose to use internationally recognized standards for character formats (Unicode®, www.unicode.org) and floating-point numbers (IEEE 754, grouper.ieee.org/groups/754/). We discuss the Unicode character formats in Appendix E, Unicode®. In the far right column of the table, we display the name of the structure or class that defines the primitive type. Note that these are the names used for the methods of class Convert that we discussed in the previous section. For instance, to convert a value to type Integer, the name of the proper Convert class method would be Convert.ToInt32, not Convert.ToInteger.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net