In our Square class so far, we were using a
constructor to initialize the value of the member variable. This meant that we had to always make
sure that we knew the value of the member variable when we declared an
instance of the class. We implemented the Radius property as read-only
and the clients of the Square class could only read the value of the
member variable. In some cases, you may not want those external procedures
or classes to read the value but only to be able to change it. To provide
this functionality, you can create a property that is referred to as
write-only.
A property is called write-only if the clients of the
class can change the value of that property but cannot read. The formula
to create a write-only property is (once again, this formula mentions only the keywords we have
reviewed so far):
Public | Private | Protected ] _ [ WriteOnly ] [ Overloads | Overrides ] _ [ Overridable ] | Shadows | Shared ] Property PropName As PropType Set(ByVal value As DataType ) End Set End Property
The WriteOnly keyword is used to indicate that the
property's value can be changed by the clients of the class but they cannot
change it. If you
are creating a write-only property, you must include the WriteOnly
keyword.
To allow clients of a class to be able to change the
value of the property, the Set statement takes an argument. Here is an example:
Public Class Circle
Private rad As Double
Public Sub New()
rad = 0
End Sub
Public Sub New(ByVal r As Double)
rad = r
End Sub
Public WriteOnly Property Radius() As Double
Set(ByVal Value As Double)
End Set
End Property
End Class
The minimum operation you can perform with a write-only
property is to assign it a value that would be provided by the outside
world. To do this, you can assign the value of the Set argument to the
corresponding member variable that the property represents. Here is an
example:
Public Class Circle
Private rad As Double
Public Sub New()
rad = 0
End Sub
Public Sub New(ByVal r As Double)
rad = r
End Sub
Public WriteOnly Property Radius() As Double
Set(ByVal Value As Double)
rad = Value
End Set
End Property
End Class
As you see, clients of a class can change the
corresponding member variable of a member variable through the Set property writer.
You may have realized that, if you create a read-only
property without the ability to write to it, the clients of a class can
only get the value of the property. On the other hand, a write-only
property restricts the ability to read the value it holds. In some rare
cases, you can keep these two functionalities separate. In most cases,
when creating a property, you would want its role to serve as a complete
"door" through which the clients of a class can read or change
the value of its hidden member variable. Such a property is create with
read-write capabilities.
A property is referred to as read-write if it allows
external classes, structures, and procedures to either change its value or
to read that value when necessary. To create a read-write property, you
must implement both the Get and the Set statements. The formula to follow
would be:
Public | Private | Protected ] [ Overloads | Overrides ] _ [ Overridable ] | Shadows | Shared ] Property PropName As PropType Get End Get Set(ByVal value As DataType ) End Set End Property
Notice that, because this is a read-write property,
you omit the ReadOnly and the WriteOnly keywords. When
implementing the property, provide the necessary functionality in the Get
and Set statements as we reviewed in the respective above sections. Here
is an example:
File: Circle.vbPublic Class Circle Private ReadOnly PI As Double = 3.14158 Private rad As Double Public Sub New() rad = 0 End Sub Public Sub New(ByVal r As Double) rad = r End Sub Public Property Radius() As Double Get If rad < 0 Then Return 0 Else Return rad End If End Get Set(ByVal Value As Double) rad = Value End Set End Property Public ReadOnly Property Diameter() As Double Get Return rad * 2 End Get End Property Public ReadOnly Property Circumference() As Double Get Return Diameter * PI End Get End Property Public ReadOnly Property Area() As Double Get Return rad * rad * PI End Get End Property End Class File: Exercise.vbModule Exercise Public Function Main() As Integer Dim circ As Circle = New Circle(64.25) Console.WriteLine(" -=- Circle Characteristics -=-") Console.WriteLine("Radius: {0}", circ.Radius) Console.WriteLine("Diameter: {0}", circ.Diameter) Console.WriteLine("Circumference: {0}", circ.Circumference) Console.WriteLine("Area: {0}" & vbCrLf, circ.Area) Return 0 End Function End Module
This would produce:
-=- Circle Characteristics -=- Radius: 64.25 Diameter: 128.5 Circumference: 403.69303 Area: 12968.63858875
To assist you with your various programming tasks, the
Visual Basic language provides many built-in classes that are equipped with many
properties.
To find out the date of the system clock of the computer on
which your application is running, you can access a property named Today. This
property is of type Date:
Public Property Today() As DateTime
This is an example of using it;
Public Module Exercise
Public Function Main() As Integer
MsgBox("Today is " & Today)
Return 0
End Function
End Module
|
|
||||||||||||||
|
The Properties of a Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment