VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

Object Methods - "Public" Procedures

This section provides a tutorial example on how to use 'Public Function/Sub' to define public methods. If a method is defined as the 'Default', it can be invoked by the object variable name without method name.

We have learned enough about object properties. Now we need to learn how to define object methods:

1. "Public" Qualifier - Declares procedures to be used as public methods accessible inside and outside this class. Here is the "Public" qualifier should be used:

   Public Function function_name(...)
      ...
   End Function
   Public Sub subroutine_name(...)
      ...
   End Sub

2. "Private" Qualifier - Declares procedures to be used private procedures accessible only inside this class. Here is the "Private" qualifier should be used:

   Private Function function_name(...)
      ...
   End Function
   Private Sub subroutine_name(...)
      ...
   End Sub

3. "Public Default" Qualifier - Declares a procedure to be the default public method accessible inside and outside this class. Here is the "Public Default" qualifier should be used:

   Public Default Function function_name(...)
      ...
   End Function
   Public Default Sub subroutine_name(...)
      ...
   End Sub

4. "." Dot Operator Followed by Method Names - Accesses public methods to assign new values or retrieve existing values. Here is the "." dot operator structure:

   expression... object_name.function_name(...)
   Call object_name.subroutine_name(...)

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

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

   document.writeln()
   document.writeln("Information of the object:")
   document.writeln("   oNode.Email: " & oNode.Email)
   document.writeln("   oNode.ToString(): " & oNode.ToString())
   document.writeln("   oNode(): " & oNode())
   document.writeln("   oNode: " & oNode)

Class Node
   Private User, Domain

   Public Default Function ToString()
      ToString = Email()
   End Function

   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
   
   Sub Class_Initialize()
      User = "user"
      Domain = "some.com"
   End Sub 
End Class
</script></pre>
</body></html>

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

Information of the object:
   oNode.Email: info@yahoo.com
   oNode.ToString(): info@yahoo.com
   oNode(): info@yahoo.com
   oNode: info@yahoo.com

Notice that how the default method can be invoked without method name, oNode() or oNode.

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

 Printable Copy - PDF Version

Object Methods - "Public" Procedures - Updated in 2015, by Dr. Herong Yang