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 Let" 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 Let" 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+$