开发者

Naming Optional Parameters in Visual Basic

In Visual Basic, I have functions with a lot 开发者_运维技巧of optional arguments. I would like to be able to pass just a few of these optional arguments to a function without having to use numerous commas and spaces to get to the ones I want. Somewhere I saw a way to named params such as OptVar:=val, but that does not seem to work. Just wondering if there is a way to do this. This would help readability.

Function foo(Optional val1 = 1, Optional val2 = 2, Optional val3 = 3) 
End Function

To use foo with only the last arg needed like this:

fud = foo( , , 4)

is a little unwieldy. It would be better if a construct like this worked:

fud = foo(val3:=4)

But this does not work.


This actually do work:

Function foo(Optional val1 = 1, Optional val2 = 2, Optional val3 = 3)
    MsgBox "val1: " & val1 & " val2: " & val2 & " val3: " & val3
    foo = val3
End Function


Private Sub Form_Load()
    MsgBox "foo returned: " & foo(val3:=4)
End Sub

You will have as output a first messagebox saying "val1: 1 val2: 2 val3: 4" and a second one with foo returned: 4

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜