Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this semantically equivalent (as far as i can see) vbscript code fail

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 which 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())

I then went to put it in a function and it fails see 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)))

It fails on Call sbText.AppendFormat_5(Nothing, sText, Arguments) with the error "Invalid procedure call or argument: 'sbText.AppendFormat_5'".

So what i don't understand is why outside a function i can pass the following types in order:

Nothing
String
Arrary

and they work but inside a function it doesn't.

Can anybody assist?

like image 559
Robert Avatar asked Feb 22 '26 07:02

Robert


1 Answers

You need to pass the array parameter by value:

Call sbText.AppendFormat_5(Nothing, sText, Arguments)
==>
Call sbText.AppendFormat_5(Nothing, sText, (Arguments))
like image 130
Ekkehard.Horner Avatar answered Feb 23 '26 23:02

Ekkehard.Horner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!