"Public/Private" Variables and Dot Operator

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:

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