0

I know that it has not any sense. But Nevertheless.

I want to create full copy of WScript.Echo

My approach is to create function with another short name.

Sub print(arguments)
    WScript.Echo arguments
End Sub

In WScript.Echo arguments are array of strings WScript.Echo Arg1, Arg2, Arg3... Optional string values to be displayed. (may be not array, but it looks like list of strings)

My question is how I can pass same argument to my function? Or may be it is not possible in general.

2
  • Are you trying to create a printf-style function, where placeholders are replaced with values? Commented Sep 25, 2014 at 13:43
  • @Bond If I understand correctly, no. I just trying to create alias to Echo function. Same function with another name, based on Echo. I just calling Echo from my function. Problem is how to pass arguments correctly. Commented Sep 25, 2014 at 13:50

2 Answers 2

1

WScript.Echo is a variadic Sub (taking an arbitrary number of arguments). You can't write such Functions/Subs/Method in VBScript. In VBA you can use a Parameter Array or the Optional keyword.

For VBScript: You could write a Sub that takes an array a as its only parameter and WScript.Echo Join(a), but I doubt that

print Array(...)

is worth (1** less letter to type) the effort.

Update: ** Obviously I can't count.

Sign up to request clarification or add additional context in comments.

1 Comment

VBA supports the Optional keyword and ParamArray. You can use those to create variadic routines. Edit: you beat me to it.
1

Below is VBS code that provides the same functionality as ParamArray modifier in VBA: it allows to pass arbitrary number of arguments, and called function receives passed arguments as array. It uses ScriptControl JScript arguments property to prepare the array of arguments.

Dim oSC, Echo

Set oSC = CreateObject("MSScriptControl.ScriptControl")
oSC.Language = "JavaScript"
oSC.AddCode "echo = function () {var dict = new ActiveXObject('Scripting.Dictionary'); for(var i=0; i<arguments.length; i++) {dict.add(i, arguments[i]);} echoArr(dict.Items());}"
oSC.AddObject "echoArr", GetRef("EchoArr"), True
Set Echo = oSC.Eval("echo")

Echo "one", "two", "three"
Echo "True variadic sub"

Function EchoArr(arr)
    WScript.Echo Join(arr)
End Function

To call WScript.Echo in variadic method manner you can use Execute, and replace EchoArr function in code above with following code (but I am not sure it has practical value):

Function EchoArr(arr)
    Dim s, i
    s = "WScript.Echo "
    i = 0
    For i = 0 to UBound(arr)
        s = s & "arr(" & i & ")"
        If i < UBound(arr) Then s = s & ", "
    Next
    Execute s
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.