"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) 2002 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?

My StringBuffer class seems to be working.

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)

 References

 Full Version in PDF/EPUB