Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Syntax VBscript中的函数是否具有可变数量的参数?_Syntax_Vbscript_Overloading - Fatal编程技术网

Syntax VBscript中的函数是否具有可变数量的参数?

Syntax VBscript中的函数是否具有可变数量的参数?,syntax,vbscript,overloading,Syntax,Vbscript,Overloading,例如,如果我在VBscript中有一个函数: Function sum(a, b, c) sum = a + b + c End function 现在,我主要创建两个变量,并将它们传递到函数sum,如下所示: Dim a : a = 1 Dim b : b = 2 Call sum(a, b) 这行不行,为什么?谢谢。根据,VBscript不支持可选参数。您可以按照他们的建议执行操作,并将空值传递给函数。VBScript不支持可选参数或方法重载。但是,您可以将空值传递给函数调用。它

例如,如果我在VBscript中有一个函数:

Function sum(a, b, c)
    sum = a + b + c
End function
现在,我主要创建两个变量,并将它们传递到函数
sum
,如下所示:

Dim a : a = 1
Dim b : b = 2
Call sum(a, b)

这行不行,为什么?谢谢。

根据,VBscript不支持可选参数。您可以按照他们的建议执行操作,并将空值传递给函数。

VBScript不支持可选参数或方法重载。但是,您可以将空值传递给函数调用。

它不起作用,VBScript不支持可选参数。
我会使用一个函数,它接受一个数字数组,而不是改变参数的个数来求和

Function sum(nums)
    Dim i, out
    For i = 0 To UBound(nums)
        out = out + nums(i)
    Next
    sum = out
End function

Call sum(Array(1, 2, 3, 4))
我希望这会有所帮助。 我使用dictionary对象将变量传递给函数,这样我就可以添加新参数,而无需重构现有代码

dim params
set params = CreateObject("Scripting.Dictionary")

'...when I want to call a function
params.add "variable_name", value: params.add "variable_name_2", value ': ...
call fn_function_name(params)


'...declaring a function
function fn_function_name(byRef params_in)

    'here I usually make sure that variable is of correct type, or that is set
    params_in("variable_name") = fn_check(params_in("variable_name"), "number") ' fn_check is a separate function

    ' ... function code goes here ...

    ' in order to user external dictionary "params" multiple times, I empty dictionary before exiting the function. This is possible because I set it as a reference (byRef) instead of value
    params_in.removeAll()
end function

即使传递对象的副本(即对数据的引用),应用于副本的更改也会影响原始副本。所以你最后的评论是误导性的。