In VB.NET, properties are nothing but natural extension of data fields. They are usually known as 'smart fields' in VB.NET community. We know that data encapsulation and hiding are the two fundamental characteristics of any object oriented programming language. In VB.NET, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.
Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.
An example,
which uses a set of set/get methods, is shown below.
'SET/GET methods
Imports System
Class [MyClass]
Private x As Integer
Public Sub SetX(ByVal i As Integer)
x = i
End Sub 'SetX
Public Function GetX() As Integer
Return x
End Function 'GetX
End Class '[MyClass]
Class MyClient
Public Shared Sub Main()
Dim mc As New [MyClass]
mc.SetX(10)
Dim xVal As Integer =
mc.GetX()
Console.WriteLine(xVal) 'Displays
10
End Sub 'Main
End Class 'MyClient
But VB.NET provides a
built in mechanism called properties to do the above. In VB.NET, properties are
defined using the property declaration syntax. The general form of declaring a
property is as follows.
<acces_modifier> <return_type> <property_name>
Get
End Get
Set
End Set
Where <access_modifier> can be private, public, protected or internal. The <return_type> can be any valid VB.NET type. Note that the first part of the syntax looks quite similar to a field declaration and second part consists of a get accessor and a set accessor.
For example the
above program can be modifies with a property X as follows.
Class MyClass
Private x As Integer
Public Property X() As Integer
Get
Return x
End Get
Set(ByVal Value As Integer)
x = value
End Set
End Property
End Class 'MyClass
The object of the class MyClass can access the property X as follows.
Dim mc As New [MyClass]
mc.X = 10 '
calls set accessor of the property X, and pass 10 as value of the standard field
"value".This is used for setting value for the data member x.
Console.WriteLine(mc.X) '
displays 10. Calls the get accessor of the property X.
The complete program is shown below.
'VB.NET:
Property
Imports System
Class MyClass
Private x As Integer
Public Property X() As Integer
Get
Return x
End Get
Set(ByVal Value As Integer)
x = value
End Set
End Property
End Class '[MyClass]
Class MyClient
Public Shared Sub Main()
Dim mc As New MyClass()
mc.X = 10
Dim xVal As Integer =
mc.X
Console.WriteLine(xVal) 'Displays
10
End Sub 'Main
End Class 'MyClient
Remember that a property should have at least one
accessor, either set or get. The set accessor has a free variable available in
it called value, which gets created automatically by the compiler. We can't
declare any variable with the name value inside the set accessor.
We can do very complicated calculations inside the set or get accessor. Even they can throw exceptions.
Since normal data fields and properties are stored in the same memory space, in VB.NET, it is not possible to declare a field and property with the same name.
Static Properties
VB.NET also supports static properties, which belongs to the class rather than
to the objects of the class. All the rules applicable to a static member are
applicable to static properties also.
The following program shows a class with a static property.
Join the conversation! Your thoughts help the community grow.