Properties
You must be wondering if we declare all the fields in our class as private, how can we assign values to them through their reference as we did in the Student class before? The answer is through Properties. VB.Net is the first language to introduce the support of defining properties in the language core. In traditional languages like Java and C++, for accessing the private fields of a class, public methods called getters (to retrieve value) and setters (to assign value) were defined like if we have a private field name
Private name As String
then, the getters and setters would be like
' getter to name field
Public Function GetName() As String
Return name
End Function
' setter to name field
Public Sub SetName(ByVal theName As String)
name = theName
End Sub
Using these we could restrict the access to a particular member. For example we can only opt to define the getter for the
totalMarks field to make it read only.
Private totalMarks As Integer
Public Function GetTotalMarks() As Integer
Return totalMarks
End Function
Hence outside the class, one can only read the value of
totalMarks and cannot modify it. You can also decide to check conditions before assigning a value to a field
Private marksOfMaths As Integer
Public Sub SetMarksOfMaths(ByVal marks As Integer)
If marks >= 0 And marks <=100 Then
marksOfMaths = marks
Else
marksOfMaths = 0
' or throw some exception informing user marks out of range
End If
End Sub
This procedure gives you a lot of control over how fields in your classes should be accessed and dealt with. The problem is this, you need to define two methods and have to prefix the name of your fields with Get or Set. VB.Net provides built in support for these getters and setters in the form of properties
"Properties are context sensitive constructs used to read, write or compute private fields of class and to achieve the control over how the fields can be accessed"
Using Properties
General Syntax for Properties is
<access modifier> Property <name of property> As <data type>
Get
' some optional statements
Return <some private field>
End Get
Set(ByVal <identifier> As <data type>)
' some optional statements
<some private field> = <identifier>
End Set
End Property
Didn't understand it? No problem lets clarify it with an example. We have a private field name
Private mName As String
To define a property for this, providing both getters and setters.
Simply type
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Set
End Property
We defined a property called
'Name' and provided both a getter and a setter in the form of a
Get...End Get and a
Set...End Set blocks. Note that we called our property
'Name' which is accessing the private member field
'mName' (where m in mName denotes the member variable name). It is a convention to name the property similar to the corresponding field but with first letter in uppercase (for
mName->Name, for
mPercentage->Percentage). As properties are accessors to certain fields, they are mostly marked as
Public while the corresponding field is (and should be) mostly marked as
Private. Finally note in the
Set...End Set block, we wrote
mName = Value
Above, a
Value is the argument that is passed when a property is called.
In our program we will use our property as
Dim theStudent As New Student()
theStudent.Name = "Faraz"
Dim myName As String = theStudent.Name
theStudent.mName = "Someone not Faraz" ' error
While defining properties, we said properties are context sensitive. When we write
theStudent.Name = "Faraz"
The compiler sees that the property
Name is on the left hand side of assignment operator, so it will call the
Set...End Set block of the property passing it
"Faraz" as Value (which is its argument). In the next line...
Dim myName As String = theString.Name
The compiler now sees that the property
Name is on the right hand side of the assignment operator, hence it will call the
Get...End Get block of property Name which will return the contents of the
Private field
mName (
"Faraz" in this case, as we assigned in line 2) which will be stored in the local string variable
myName. Hence, when the compiler finds the use of a property, it checks in which context it is called and takes the appropriate action with respect to the context. The last line
theStudent.mName = "Someone not Faraz" ' error
will generate a compile time error (if called outside the Student class) as the
mName field is declared
Private in the declaration of the class.
You can give the definition of either of
Get...End Get or
Set...End Set block. If the
Get...End Get block is missing then the property must be marked as WriteOnly and similarly If the
Set...End Set block is missing then the property must be marked as ReadOnly.
Public WriteOnly Property Password() As String
Set(ByVal Value As String)
mPassword = Value
End Set
End Property
And,
Public ReadOnly Property Salary() As String
Get
Return mSalary
End Get
End Property
If you miss one of these, and user tries to call it, he/she will get the compile time error. For example,
Dim mySalary As String
Employee.Salary = mySalary
The above lines will cause a compile time error (supposing the Salary property above is defined in the Employee class) as we did not provide any
Set...End Set block of the
Salary property.
You can also write statements in the
Get...End Get or
Set...End Set blocks as you do in methods.
Private mMarksOfMaths As Integer
Public WriteOnly Property MarksOfMaths() As Integer
Set(ByVal Value As Integer)
If Value >= 0 And Value <= 100 Then
marksOfMaths = Value
Else
marksOfMaths = 0
' or throw some exception informing user marks out of range
End If
End Set
End Property