VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.00

"StringBuffer" - A Class Example

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) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<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?

My StringBuffer class seems to be working.

Sections in This Chapter

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

Dr. Herong Yang, updated in 2008
"StringBuffer" - A Class Example