如何在Visual Basic中使用可选参数进行应用
以下代码接收一系列字符串,并使用分隔符将它们连接起来。
Function concat(range, sep)
mystring = ""
For Each item In range
mystring = mystring & sep & item
Next
mystring = Right(mystring, Len(mystring) - Len(sep)) 'removes preceding separator
concat = mystring
End Function
我希望sep是一个可选参数,所以我可以调用concat(range)而不是concat(range;“”)
python中的示例:
def concat(range, sep="")
mystring = ""
for i in range:
mystring += sep + i
mystring = mystring[len(sep)::]
return mystring
答案 0 :(得分:3)
您需要声明类型。
使用关键字Optional
Function concat(rng As range, Optional sep As String = "")
Dim mystring As String
Dim itm As range
mystring = ""
For Each itm In rng
mystring = mystring & sep & itm
Next
mystring = Right(mystring, Len(mystring) - Len(sep)) 'removes preceding separator
concat = mystring
End Function