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