Reference Types and Classes

 <  Day Day Up  >  

As discussed in the section on memory management, references to values on the heap can be stored in variables just as regular values can. A reference type is always stored on the heap. Thus, a variable of a reference type never contains its value directly ”it always contains a reference to its value on the heap.

A class is a user -defined reference type. For example, the following declares a new Customer class that contains name , address, and phone number information about a customer.

 Class Customer   Public Name As String   Public Address As String   Public PhoneNumber As String End Class 

Before a reference type variable is assigned a reference, it contains a special reference called the null reference . The null reference represents a reference to nothing and is referred to, appropriately enough, by the keyword Nothing . Because classes are reference types, the initial value of a variable typed as a class will be Nothing . Before a reference type variable can be used, it first has to be assigned a reference to a value on the heap. The New operator creates a new instance of a class on the heap.

 Dim x, y As Customer ' Error: x is Nothing x.Name = "John Doe" ' OK: Creates a new instance of Customer y = New Customer() y.Name = "Jane Doe" 

New can also be specified inline when a variable is declared. This is the equivalent of a variable initializer assigning a new expression. In the following example, the two declaration lines are completely equivalent.

 Dim x As New Customer Dim y As Customer = New Customer() 

NOTE

Remember that initializers are run when execution reaches the declaration line. If the preceding lines were executed multiple times, the variables would be initialized multiple times. If the preceding lines were skipped , the variables would not be initialized .


Using a reference type variable at runtime that contains the null reference will result in a System.NullReferenceException exception. Reference type variables can be compared against Nothing using the Is operator to determine whether they contain a reference to the null reference. Assigning Nothing to a variable causes the reference it contains, if any, to be discarded. For example:

 Module Test   Sub SetName(ByVal c As Customer)     ' Make sure c refers to something     If c Is Nothing Then       Console.WriteLine("c has no value.")       Return     End If     c.Name = "John Doe"   End Sub   Sub Main()     Dim c As New Customer()     SetName(c)     c = Nothing   End Sub End Module 

Style

In general, it is best to set reference variables to Nothing as soon as you are finished with them so that the memory can be reclaimed by the garbage collector. However, a variable that reaches the end of its lifetime (for example, a local at the end of a subroutine) is automatically set to Nothing by the Framework and does not need to be set to Nothing .


When you assign a variable that contains a reference to another variable, the only thing that gets copied is the reference ”not the value itself. This is important to remember, because it can have a big effect on the behavior of the variables. Take the following example.

 Class b   Public x As Integer End Class Module Test   Sub Main()     Dim a1, a2 As Integer     Dim b1, b2 As b     b1 = New b()     a1 = 10     b1.x = 10     a2 = a1     b2 = b1     a1 = 20     b1.x = 20     Console.WriteLine(a2)     Console.WriteLine(b2.x)   End Sub End Module 

This program will print the values 10 and 20 . This is because the variables a1 and a2 directly contain Integer values, while the variables b1 and b2 store their Integer values on the heap. When a1 is assigned to a2 , this copies the value from a1 to a2 . Thus, when a1 is changed, there is no effect on a2 . But when b1 is assigned to b2 , this copies the reference to the value from b1 to b2 . When the value that b1 refers to is changed, the value that b2 refers to is changed as well because they both point to the same value.

 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

Similar book on Amazon

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