
Field Initialization
VB automatically initializes fields of a class to default values. The assigned default value depends on the field’s
type (integer, string, etc.)
'VB automatically sets all field members to a safe default value.
Public Class DefaultValues
'Here are a number of fields and the corresponding defaults
Private theByte As Byte ' = 0
Private theShort As Short ' = 0
Private theInt As Integer ' = 0
Private theLong As Long ' = 0
Private theChar As Char ' = 0
Private theSingle As Single ' = 0.0
Private theDouble As Double ' = 0.0
Private theBool As Boolean ' = False
Private theDecimal As Decimal ' = 0
Private theStr As String ' = Nothing
Private theObj As Object ' = Nothing
End Class
You can also initialize fields in the class declaration. This is helpful if a field should default to a value that is
different from the runtime-supplied default. Without this ability, each constructor would need to call a common
helper function to initialize the fields.
'This technique is useful when you don’t want to use the supplied
'default values and don’t want to write the same code in each ctor.
Public Class Test
Private myInt As Integer = 90
Private myStr As String = "My initial value"
Private emp As New Employee()
End Class
Implementing Class Properties
To promote encapsulation, you should make class fields private. Since nothing outside the class can access
private members, the data is safe from misuse. But how useful is an object whose state cannot be modified? Not
very.
It is common practice to implement special methods that allow controlled access to the private fields. In some
circles, these methods are called getters and setters, or in more highbrow language: accessors and mutators.
'Traditional accessor and mutator for private data.
Public Class Employee
...
Private mWage As Double
'Accessor (getter).
Public Function GetWage() As Double
Return mWage
End Function
'Mutator (setter).
Public Sub SetWage(ByVal wage As Double)
'Enforce business rule: wage value must be less than $20.
If wage < 20 Then
mWage = wage
End If
End Sub
End Class
Here is some code that uses the getter and setter:
'Getter / setter usage.
Sub Main()
Dim emp As New Employee()
emp.SetWage(15.5)
Console.WriteLine("Employee's wage is: {0}", emp.GetWage())
End Sub
VB supplies a syntactical construct called a property that makes writing and using getters and setters easier.
Here is the wage property syntax that is equivalent to the traditional getter/setter technique. It is a .NET best
practice to encapsulate fields of a class using property syntax, as opposed to the previous getter/setter syntax
we just examined. Although VB 6.0 also supported properties, the syntax under VB is very different.
Public Class Employee
...
Private mWage As Double
' VB .NET property syntax.
Public Property Wage() As Double
Get
Return mWage
End Get
Set(ByVal Value As Double)
'Enforce business rule: wage value must be less than $20.
If Value < 20 Then
mWage = Value
End If
End Set
End Property
End Class
To the object consumer, a property looks like a publicly exposed field. In reality, the class developer controls
all access by implementing validation logic within the property.
'Property Usage
Sub Main()
Dim emp As New Employee()
'Invokes Wage set block. 15.5 passed as Value argument
emp.Wage = 15.5
'Invokes Wage get block
Console.WriteLine("Employee's wage is: {0}", emp.Wage)
End Sub
You can also create read-only and write-only properties using the appropriate keywords. ReadOnly is used to
mark a read-only property. Notice there is no ‘set’ scope. WriteOnly is for write-only properties, which do
not support a ‘get’ scope.
Class Employee
...
Public ReadOnly Property Name() As String
Get
Return mName
End Get
End Property
Public WriteOnly Property Hours() As Double
Set(ByVal Value As Double)
mHours = Value
End Set
End Property
End Class
VB 6.0 programmers were able to create properties which had different visibility levels. For example, a public
Property Get and a private Property Set. In .NET, The access modifier used on the property definition will be
the least restrictive, while the get / set scope will specify the more restrictive. For example, the following
property can be only set by derived types of the Car itself.
Public Class Car
Private currSpeed As Integer
' Anyone can get the speed,
' but only derived types can set it.
Public Property Speed() As Integer
Get
Return currSpeed
End Get
Protected Set(ByVal value As Integer)
currSpeed = value
End Set
End Property
End Class
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Field Initialization, Class Properties
Table of Contents
Courseware
Training Resources
Tutorials