This section provides a tutorial example on how to declare public properties for external use and private variables for internal use. The dot, '.', operator allows you to access public properties.
The classes defined in previous sections are empty class, because there are no variables declared
in them to store values.
To declare class-level variables, we need to learn few more things:
1. "Public" Statement - Declares variables to be used as public properties.
A public property can be used inside and outside this class.
Here is the "Public" statement structure:
Class class_name
Public variable1, variable2, ...
'Other statements
End Class
2. "Private" Statement - Declares variables to be used as private variables.
A private property can be used only inside this class.
Here is the "Private" statement structure:
Class class_name
Private variable1, variable2, ...
'Other statements
End Class
3. "Dim" Statement - Declares variables to be used as public properties, if used at the class level.
A public property can be used inside and outside this class.
Here is the "Private" statement structure:
Class class_name
Dim variable1, variable2, ...
'Other statements
End Class
4. "." Dot Operator Followed by Property Names - Accesses public properties
to assign new values or retrieve existing values.
Here is the "." dot operator structure:
object_name.property_name = value
expression...object_name.property_name...
Now I am ready to show you a class with public properties and private variables:
<html><body>
<!-- Class_Variables.html
- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre><script language="vbscript">
' Initiating an object from a class
Dim oNode
Set oNode = New Node
' Getting values from public properties
document.writeln("oNode.NodeValue: " & oNode.NodeValue)
document.writeln("oNode.NodeType: " & oNode.NodeType)
' Accessing private variable is not allowed
' document.writeln("oNode.CreateTime: " & oNode.CreateTime)
' Setting new values to public properties
oNode.NodeValue = "VBScript"
oNode.NodeType = "Language"
document.writeln("oNode.NodeValue: " & oNode.NodeValue)
document.writeln("oNode.NodeType: " & oNode.NodeType)
' Removing the object reference
Set oNode = Nothing
' Defining a class with variables
Class Node
Dim NodeType ' Public by default
Public NodeValue
Private CreateTime
Private Messages(9)
Sub Class_Initialize()
CreateTime = Time()
NodeValue = "HerongYang.com"
NodeType = "Domain"
Messages(0) = "This is a private message."
Messages(1) = "Outside access is not allowed."
End Sub
Sub Class_Terminate()
document.writeln(Messages(1))
Erase Messages
End Sub
End Class
</script></pre>
</body></html>
When you load this VBScript example into IE, you will get this output:
oNode.NodeValue: HerongYang.com
oNode.NodeType: Domain
oNode.NodeValue: VBScript
oNode.NodeType: Language
Outside access is not allowed.
Note that:
Access to private variable "CreateDate" from outside of the class is not allowed.
"Class_Terminate" is used to erase the private array variable "Messages" to release more storage.