"Property Let/Set/Get" Procedures

This section provides a tutorial example on how to use 'Property Let/Set/Get' procedure to define public properties.

Properties defined through public variables are simple to use. But you can not use them to update internal variables or other properties when their values are changes.

A better way to define public properties is to use "Property" procedures with following statements:

1. "Property Let" Statement - Defines a property procedure to allow values, not objects, to be assigned to a property. Here is how a "Property Let" statement should used:

Public Property Let property_name(value)
   ...
   ... Exit Property
   ...
End Property 

2. "Property Set" Statement - Defines a property procedure to allow objects, not values, to be assigned to a property. Here is how a "Property Set" statement should used:

Public Property Set property_name(object)
   ...
   ... Exit Property
   ...
End Property 

3. "Property Get" Statement - Defines a property procedure to return values or objects of a property. Here is how a "Property Get" statement should used:

Public Property Get property_name()
   ...
   property_name = ... ' If it is a value property
   Set property_name = ... ' If it is an object property
   ...
   ... Exit Property
   ...
End Property 

To show you how to define public properties with property procedures, I wrote this VBScript example:

<html><body>
<!-- Class_Properties.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre><script language="vbscript">
   Dim oNode
   Set oNode = New Node

   ' Getting values from public properties
   document.writeln("oNode.Title: " & oNode.Title)
   document.writeln("oNode.Email: " & oNode.Email)
   document.writeln("oNode.DomainRegExp.Pattern: " _
      & oNode.DomainRegExp.Pattern)

   ' Setting new values to public properties
   oNode.Title = "Microsoft Help Desk"
   oNode.Email = "help@microsoft.com"

   Set oRegExp = New RegExp
   oRegExp.Pattern = "\.\w+$"
   Set oNode.DomainRegExp = oRegExp

   document.writeln("oNode.Title: " & oNode.Title)
   document.writeln("oNode.Email: " & oNode.Email)
   document.writeln("oNode.DomainRegExp.Pattern: " _
      & oNode.DomainRegExp.Pattern)

' Defining a class with properties
Class Node
   Public Title
   Private myObject
   Private User, Domain ' Used to support "Email" property

   Public Property Let Email(sEmail)
      at = InStr(sEmail, "@")
      If at>0 Then
         User = Mid(sEmail, 1, at-1)
         Domain = Mid(sEmail, at+1, Len(sEmail)-at)
      End If
   End Property

   Public Property Get Email()
      Email = User & "@" & Domain
   End Property

   Public Property Set DomainRegExp(oRegExp)
      Set myObject = oRegExp
   End Property

   Public Property Get DomainRegExp()
      Set DomainRegExp = myObject
   End Property
   
   Sub Class_Initialize()
      Title = "Yahoo Information Center"
      User = "info"
      Domain = "yahoo.com"
      Set myObject = New RegExp
      myObject.Pattern = "\..+$"
   End Sub 
End Class
</script></pre>
</body></html>

When you load this VBScript example into IE, you will get this output:

oNode.Title: Yahoo Information Center
oNode.Email: info@yahoo.com
oNode.DomainRegExp.Pattern: \..+$
oNode.Title: Microsoft Help Desk
oNode.Email: help@microsoft.com
oNode.DomainRegExp.Pattern: \.\w+$

Last update: 2016.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

Creating Your Own Classes

 Class, Property, Method and Related Statements

 "Class" Statement - Defining Your Own Class

 "New" Operator and "Nothing" Object

 "Public/Private" Variables and Dot Operator

"Property Let/Set/Get" Procedures

 Object Methods - "Public" Procedures

 "New", "Set", "Is", ".", "Nothing" - Object Operations

 "StringBuffer" - A Class Example

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 PDF Printing Version