|
||||||
Using a Property Let and Get in a VBScript ClassHow to Work with an Object's Properties and VBScript ProgrammingA property in a VBScript class is some much more than just a variable. A programmer can add code to the get and let statements that will control the object's operations
Any object oriented programming class, VBScript or otherwise, will have properties and each property will need to be set. For example a class used to represent a person may have:
The properties can be included in a class as simple variables however, in the case of VBScript, a property:
This additional functionality can be used to run rules on the information being assigned to the class, for example:
All of this can, of course, be incorporated into a VBScript class. A Simple VBScript ClassThe simplest VBScript will simply define each of the properties as a public variable: Class Person
public age
public gender
public first_name
public surname
public prefix
public marital_status
End Class
It's then up to the programmer to set the details with the correct information: Set person_1 as New Person
person_1.gender = "male"
person_1.prefix = "Mr"
However that be automated by using the VBScript property let and get statements. Using the VBScript Property Let StatementThe Property Let statement is used to define the actions that occur when a value is assigned to the property. So, for example, the class may contains some private variables: Class Person
Private my_age
Private my_gender
Private my_first_name
Private my_surname
Private my_marital_status
Private my_prefix
The programmer can then update these by creating a property let subroutine, for example: Public Property Let gender (new_gender)
my_gender = new_gender
If my_gender = "male" Then
my_prefix = "Mr"
Else
Select Case my_marital_status
Case "married"
my_prefix = "Mrs"
Case "unmarried"
my_prefix = "Miss"
Case Else
my_prefix = "Ms"
End Select
End If
End Property
Here two variables are set:
This means, of course, that all of the functionality is built into the class rather than the application that uses the class. Using the VBScript Property Get StatementThe get statement is used to retrieve the value of the property. However, just the like the let statement, it can have additional functionality built into it. Unlike the let statement it does not need an input. For example, it is useful for setting default values: Public Property Get prefix
If my_prefix = "" Then
prefix = "unknown"
Else
prefix = my_prefix
End If
End Property
End Class
Public Property Get gender
If my_gender = "" Then
gender = "unknown"
Else
gender = my_gender
End If
End Property
Of course, one of the easiest ways to understand the property let and set statements is to see them in action. Using the VBScript Get and Let Statements TogetherOnce all of the property let and get procedures have been set then the class itself starts to become quite intelligent: Set person_1 = New Person
person_1.gender = "male"
Wscript.echo "Person 1 is a " & person_1.prefix & " and " & person_1.gender
Set person_2 = New Person
person_2.gender = "female"
Wscript.echo "Person 2 is a " & person_2.prefix & " and " & person_2.gender
The result (as shown in figure 1) is that the new object can work out the correct prefix according to the information that is given to it. And that, of course, is the key to object oriented programming: any decision making should be done by the class and not the program using the class. In that way a class will always operate in the same way regardless of where it is used.
The copyright of the article Using a Property Let and Get in a VBScript Class in Windows Programming is owned by Mark Alexander Bain. Permission to republish Using a Property Let and Get in a VBScript Class in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||