Monday, February 16, 2009

Fast String in ASP

ASP is an old framwork and really outdated but sometimes we still need to use it.  One major problem is the handling of strings.  We recently worked on a project with ASP and tried to use the standard string method to create some reports.  Basically, a report with 1,000 entries timedout, until we set the server timeout to 10 minutes...!

So here is a way to overcome this.  This is a class that you can use if you want to create strings and need to concatenate them.

Class FastString
Dim stringArray,growthRate,numItems
Private Sub Class_Initialize()
growthRate = 50: numItems = 0
ReDim stringArray(growthRate)
End Sub
Public Sub Append(ByVal strValue)
' next line prevents type mismatch error if strValue is null. Performance hit is negligible.
strValue=strValue & ""
If numItems > UBound(stringArray) Then ReDim Preserve stringArray(UBound(stringArray) + growthRate)
stringArray(numItems) = strValue:numItems = numItems + 1
End Sub
Public Sub Reset
Erase stringArray
Class_Initialize
End Sub
Public Function concat()
Redim Preserve stringArray(numItems)
concat = Join(stringArray, "")
End Function
End Class 

I will give full credit to: A Fast String Class for ASP Pages

If you want to implement this class, do something like this:

Set mystring = New FastStrign
mystring.Append("My Info")
mystring.concat()
Set mystring = nothing

This will add "My Info" to the string and then print it.  The last line is to detroy the old thing, you can also use the build in erase function.  This will make concatenating of strings much faster.

Remember I said 10 minutes...? Now it takes 20 seconds to create the report!

No comments: