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