如何在visual basic中创建具有可变数目参数的函数?例如。
x = Sum(1,2,3)
y = Sum(1,2)
Function Sum('how to declare argument here')
'Is there any special argument manipulation inside function before it is usable?
End Function
发布于 2010-04-13 22:18:54
看一看Passing a Variable Number of Arguments
Function Sum(ParamArray Vals() As Variant)
Dim intLoopIndex As Integer
For intLoopIndex = 0 To UBound(Vals)
Next intLoopIndex
End Function
发布于 2010-04-13 22:20:59
使用可选参数,例如:
Function Sum(Optional X1 As Integer=0, Optional X2 As Integer=0)
或通用变量参数语法
Function Sum(ParamArray XArr() As Variant)
(我可能弄乱了一些语法元素-请随意更正。)
发布于 2014-08-29 12:36:01
这里的答案很棒。在我的应用程序中,我要求在一个必需的初始参数之后有一个任意长的可选参数列表。
您只需在ParamArray条目之前添加所需的参数即可完成此操作。
例如:
Function Arithmetic(FuncType As String, ParamArray Terms() As Variant)
https://stackoverflow.com/questions/2630171
复制相似问题