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

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

This section provides a tutorial example on how to use object operations, 'New', 'Set', 'Is', '.' and 'Nothing'. The default method of an object can be invoked without method name.

Now we know how to define a class, its properties and its methods. Let's summarize operations that related to class objects:

  • "New class_name" - The "New" operator creates a new object and returns its reference. The Class_Initialize() procedure will be called.
  • "Set variable = object_reference" - The "Set" statement assigns the object reference to the variable.
  • "object_reference.property_name" - The dot "." operator access the object property.
  • "object_reference.method_name(...)" - The dot "." operator access the object method.
  • "object_reference(...)" - The default method call.
  • "object_reference_1 Is object_reference_2" - The "Is" operator compares two object references.
  • "Set variable = Nothing" - The "Nothing" object helps to release the reference of the previous object stored in the variable.

The VBScript example below shows how those object operations work:

<html><body>
<!-- Object_Operations.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)

   Dim secondNode
   Set secondNode = New Node
   secondNode.Email = "help@microsoft.com"
   Set oNode.Child = secondNode
   
   Dim thirdNode
   Set thirdNode = New Node
   thirdNode.Email = "sales@sun.com"
   Set secondNode.Child = thirdNode
   
   document.writeln()
   document.writeln("List of objects:")
   Set o = oNode
   Do While Not o Is Nothing
      document.writeln("   o.ToString(): " & o.ToString())
      Set o = o.Child
   Loop

Class Node
   Public Child
   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"
      Set Child = Nothing
   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

List of objects:
   o.ToString(): info@yahoo.com
   o.ToString(): help@microsoft.com
   o.ToString(): sales@sun.com

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

"New", "Set", "Is", ".", "Nothing" - Object Operations - Updated in 2015, by Dr. Herong Yang