Using a Property Let and Get in a VBScript Class

How to Work with an Object's Properties and VBScript Programming

© Mark Alexander Bain

Jul 26, 2009
Using a Property Let and Get in a VBScript Class, Mark Alexander Bain
A 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:

  • age
  • gender (male or female)
  • prefix (Mr, Ms, Mrs or Miss)
  • first name
  • surname
  • marital status

The properties can be included in a class as simple variables however, in the case of VBScript, a property:

  • is a method that sets or returns a value
  • can have additional functionality.

This additional functionality can be used to run rules on the information being assigned to the class, for example:

  • a male always has the prefix "Mr" and a prefix of "Mr" means that the person is male
  • a female may be:
      -
    • Mrs" if she is married
    • "Miss" if she is not married
    • "Ms" if the marital status is unknown

All of this can, of course, be incorporated into a VBScript class.

A Simple VBScript Class

The 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 Statement

The 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:

  • the gender is set
  • the prefix is set according to the gender and the marital status

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 Statement

The 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 Together

Once 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.


Using a Property Let and Get in a VBScript Class, Mark Alexander Bain
Figure 1: A Simple VBScript Class Property, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo