Naming Advanced Parameters in Visual Basic

In Visual Basic, I have functions with a lot of optional arguments. I would like to be able to pass only some 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 for named parameters like OptVar:=val, but that doesn't seem to work. Just wondering if there is a way to do this. This will help readability.

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

Use foofor which only the last argument is needed:

fud = foo( , , 4)

a little cumbersome. It would be better if such a design worked:

fud = foo(val3:=4)

But that does not work.

+3
source share
1

:

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

"val1:1 val2: 2 val3: 4", foo: 4

+4

Source: https://habr.com/ru/post/1750210/


All Articles