是否有办法为vb脚本中的函数创建可选参数,允许您编写类似......的函数
myFunc("happy")
myFunc("happy", 1, 2, 3)
myFunc("happy", 1)
etc.
答案 0 :(得分:14)
vbscript
中不允许使用可选关键字(如VB6)也许这会有所帮助:http://www.4guysfromrolla.com/webtech/071801-1.shtml
答案 1 :(得分:3)
overloading怎么样?
答案 2 :(得分:2)
您可以随时将其设为class
并使用Public Property Let
填充Sub
/ Function
,然后再致电:
Set oSubName = New cSubName
'fill your parameters, you can always add more later
oClass.OptionalParameter1 = true
oClass.OptionalParameter2 = false
'execute sub
oSubName.Execute
Set oSubName = Nothing
这需要一些关于如何创建类的知识,但可能是使用数组的下一个最佳解决方案。
祝你好运。答案 3 :(得分:1)
显然这取决于您的环境以及它是否支持在同一个文件中同时使用JScript和VBScript,但我使用Windows脚本宿主(* .wsf文件)取得了一些成功,即
<?xml version="1.0" standalone="yes" ?>
<package xmlns="Windows Scripting Host">
<job id="param">
<?job debug="true"?>
<script language="JavaScript">
<![CDATA[
function MakeString(args) {
var s = [];
for(var i = 0, length = arguments.length; i < length; i++)
s.push(arguments[i]);
return s.join('');
}
]]>
</script>
<script language="vbscript">
<![CDATA[
WScript.Echo MakeString("hello", " ", "world")
WScript.Echo MakeString()
WScript.Echo MakeString(1,2,3,4)
]]>
</script>
</job>
</package>
您可以在JScript中定义函数并在VBScript中引用它。更好的方法是将JScript函数作为外部文件包括
<script language="JavaScript" src="makestring.js"/>
<script language="vbscript">
<![CDATA[
WScript.Echo MakeString("hello", " ", "world")
WScript.Echo MakeString()
WScript.Echo MakeString(1,2,3,4)
]]>
</script>
答案 4 :(得分:-5)
只需传递不同类型即可。
wscript.echo "1: " & math(7,8,false)
wscript.echo "2: " & math(7,8,5)
wscript.echo "3: " & math(15,false,5)
function math( Addend1 , Addend2, Divisor )
dim tmpTotal
if Addend2 then
TmpTotal = Addend1 + Addend2
else
tmpTotal = Addend1
end if
if Divisor then 'if argument Divisor is other than False then do it.
if Divisor > 0 then 'Hated Divide by Zero since VIC-20.
tmpTotal = tmpTotal / Divisor)
end if
end if
end function