"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:

The VBScript example below shows how those object operations work:

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

   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("List of objects:")
   Set o = oNode
   Do While Not o Is Nothing
      document.writeln("   o.ToString(): " & o.ToString())
      Set o = o.Child

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

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)


 Full Version in PDF/EPUB