This section provides a tutorial example of simple class, StringBuffer, which allows you to build long strings with an internal array as the storage.
To help you review what you have learned about creating your own class,
I wrote this simple class called "StringBuffer", which is included in this test script page:
<html><body>
<!-- String_Buffer_Class.html
- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre><script language="vbscript">
Dim oBuffer
Set oBuffer = New StringBuffer
oBuffer.Append("Hello")
oBuffer.Append(" Herong,")
oBuffer.Append(vbCrLf)
oBuffer.Append("Is this StringBuffer class faster")
oBuffer.Append(" than the & operation?")
Dim sFinal
sFinal = oBuffer
document.writeln()
document.writeln("The final string with " & Len(sFinal) _
& " characters:")
document.writeln(sFinal)
Class StringBuffer
Private Buffer(), Capacity, Index
Public Default Function ToString()
ToString = Join(Buffer, "")
End Function
Public Sub Append(vAny)
sString = CStr(vAny)
Buffer(Index) = sString
Index = Index + 1
If Index = Capacity Then
Capacity = Capacity + 10
ReDim Preserve Buffer(Capacity-1)
End If
End Sub
Private Sub Class_Initialize()
Capacity = 10
ReDim Buffer(Capacity-1)
Index = 0
End Sub
Private Sub Class_Terminate()
Erase Buffer
End Sub
End Class
</script></pre>
</body></html>
When you load this VBScript test page into IE, you will get this output:
The final string with 70 characters:
Hello Herong,
Is this StringBuffer class faster than the & operation?