7.11. Value Types and Reference TypesAs 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.
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. |