|< Day Day Up >|
A value type is a type that stores its information directly in a variable or another type. The fundamental types Integer , Double , and Date are examples of value types, so for example, a local variable of type Integer that has the value 5 contains the value 5 itself rather than a reference to it, as in Figure 9-2. Similarly, an array of Integer values contains the values themselves rather than references to them, as in Figure 9-3.
Figure 9-2. An Integer Variable
Figure 9-3. An Integer Array
Because value types are always stored directly in variables , they use memory efficiently . As shown earlier, an Integer local variable does not require any space on the heap, because the integer value can be stored directly on the stack. This also means that the storage for a value type never has to be allocated, because it's always part of something else. Because variables that hold value types directly contain their value, they must always have a value. The Framework initializes all value type variables to a "zero state," or their default value , when they are created. Thus, the value of an Integer variable that has not been assigned a value will be zero.
Structures are user -defined value types. For example, the following declares a new Customer structure that contains name , address, and phone number information about a customer.
Structure Customer Public Name As String Public Address As String Public PhoneNumber As String End Structure
Because structures are value types, they do not have to be created before they can be used.
Dim x As Customer ' OK: structures do not have to be created x.Name = "John Doe"
|< Day Day Up >|