Fields

 <  Day Day Up  >  

A field is a variable that lives in a class or a structure instead of inside a method. Fields are declared in exactly the same way that local variables are declared.

 Module Test   Dim x As Integer   Dim y As Long = 5   Dim z(5) As Integer End Module 

Instance fields live as long as the class or structure that contains them lives, while shared fields live for the length of the program's execution. Fields declared in modules are equivalent to shared fields, since modules live for the entire length of the program. The following example uses a field to keep track of how many lines have been read from the console.

 Module Test   Dim LineCount As Integer = 0   Const MaxLines As Integer = 30   Sub Main()     Dim Line As String = Console.ReadLine()     While (Line <> "") AndAlso (LineCount < MaxLines)       Line = Console.ReadLine()       LineCount += 1     End While     Console.WriteLine("Read in " & LineCount & " lines.")   End Sub End Module 

The example also shows the use of constant fields, which are equivalent to constant locals.

Read-Only Fields

Constants provide a useful way to represent values that do not change, but they are limited by the fact that they can only be initialized by a constant expression. Therefore, constants cannot be initialized by the result of a function call or by the result of an object instantiation expression. In some situations, though, a value will be constant throughout a program, but the value cannot be expressed in a constant expression. In the following example, the field MyAddress might not change throughout the entire program, but because it requires an instance to be constructed , the field cannot be declared as a constant field.

 Structure Address   Public Street, City, State, ZIP As String   Sub New(ByVal Street As String, ByVal City As String, _     ByVal State As String, ByVal ZIP As String)     Me.Street = Street     Me.City = City     Me.State = State     Me.ZIP = ZIP   End Sub End Structure Module Test   Public MyAddress As Address = _     New Address("120 Main Street", "Durham", "NC", "27706")   Sub Main()     ...   End Sub End Module 

An alternative to declaring a constant field, however, is to declare a read-only field. Read-only fields can only be assigned a value once, when the class or structure is being initialized, and then are read-only afterward. The previous module could have been written as follows instead.

 Module Test   Public ReadOnly MyAddress As Address = _     New Address("120 Main Street", "Durham", "NC", "27706")   Sub Main()     ...   End Sub End Module 

In this example, the read-only field MyAddress is initialized with a new instance when the module is initialized. Once the module has been initialized, the MyAddress field cannot be changed. Read-only fields can only be assigned to by an initializer or within the constructor for a type.

Read-only fields can also be used when you are declaring fields that should not change over the lifetime of an instance of an object. In the preceding example, it may be that once an Address structure has been created, the fields in the structure should never change. This could be enforced by declaring the structure with read-only fields.

 Structure Address   Public ReadOnly Street, City, State, ZIP As String   Sub New(ByVal Street As String, ByVal City As String, _     ByVal State As String, ByVal ZIP As String)     Me.Street = Street     Me.City = City     Me.State = State     Me.ZIP = ZIP   End Sub End Structure 

Advanced

A type whose fields are all read-only is called an immutable type because once an instance of the type has been created, the value contained within it cannot change.


 <  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