Sunday, August 5, 2007

VBScript- Property Let, Property Get, Property Set

Class properties in VBScript are used to assign values to private variable and handle the process of data validation.

Property Let: Which is used by the outside code to store a value in the private property variable. It is similar to a procedure in the sense that it does not return a value. A Property Let procedure must accept at least one argument. If the private variable you are using is an object then the process of assignment and data validation is handled by Property Set. Property Set: Similar to Property Let but used for object based properties. By default, the Property Set procedure is Public.

To retrieve the value of a private variable we will retrieve the value of a property. Property Get: This is used by code outside of your class to read the value of a private property variable. It is similar to a function in the sense that it returns a value to the calling code -- this value is the private variable value.

The Property Get procedure does not accept any arguments. You can add an argument to it, but then you have to add an additional argument to the property's corresponding Property Let or Property Set procedure, because Property Let/Set procedure must always have exactly one more argument than its corresponding Property Get procedure.

If the property get procedure returns an object then we can use the set statement (but it works well without set also) to return the value.

Read only Properties have only Property Get procedure

Write-only properties have only a Property Let or a Property Set procedure

Read-Write properties have a Property Get procedure and either a Property Let or a Property Set procedure

Example 1 of Property Let, Property Get, Property Set

Below Example, which shows a simple class that defines a private variable, m_var, and a two read-write properties, one_type and two_type, the latter of which is an object

Example 2 of Property Set

Here is the syntax for a Property Set procedure.

For example, here is what code that is using an object based on the above class might look like.

Last line uses the Set Statement when it writes to the FSPro property. This is required because the Main_class class used a Property Set procedure for the FSPro property. Without the Set statement at the beginning of the last line, VBScript would produce an error. When a property on a class is object based, it is typical to use a Property Set procedure. Most programmers using this class would expect this.

Example 3 of Property Set

For example imagine we had a class that contained a private property named ob_var_conn that was expected to be an ADO Connection object. This class definition, with the
property Set and Property Get Statements might look like:

The end developer would use the Property Set statement in the following manner:

As with the Property Let statement, the Property Set statement has an optional argument list. This argument list must be identical to the corresponding Property Get's argument list.