Why is this semantically equivalent (as far as I can see) vbscript code fails
I want to create a FormatString function for VBScript that works the same as String.Format in .Net.
I found I can use the System.Text.StringBuilder object in VBScript and tested the following code that works
Option Explicit
Dim sbText 'As System.Text.StringBuilder
Set sbText = CreateObject("System.Text.StringBuilder")
Call sbText.AppendFormat_5( _
Nothing, _
"My name is {0} and the current date time is '{1:dd MMMM yyyy HH:mm:ss}'", _
Array("Robert", Now))
Call MsgBox(sbText.ToString())
Then I went to put it in a function and it doesn't show below
Option Explicit
Function FormatString(ByVal sText, ByVal Arguments) 'As String
Dim sbText 'As System.Text.StringBuilder
'Test the input variables
If Not TypeName(sText) = "String" Then _
Err.Raise 5 'vbErrInvalidProcCallOrArg
If Not IsArray(Arguments) Then _
Err.Raise 5 'vbErrInvalidProcCallOrArg
Set sbText = CreateObject("System.Text.StringBuilder")
Call sbText.AppendFormat_5(Nothing, sText, Arguments)
FormatString = sbText.ToString()
End Function
Call MsgBox(FormatString( _
"My name is {0} and the current date time is '{1:dd MMMM yyyy HH:mm:ss}'", _
Array("Robert", Now)))
Error Call sbText.AppendFormat_5(Nothing, sText, Arguments)
with error "Invalid procedure call or argument:" sbText.AppendFormat_5 ".
So I don't understand why outside the function I can pass the following types in order:
Nothing
String
Contrary
and they work, but inside a function that it is not.
Can anyone please help?
+3
source to share