2

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.

3
  • It is easier to give an answer if you provide some code that you have tried. Commented Jun 17, 2010 at 13:24
  • 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 unweildy. It would be better if a construct like this worked: fud = foo(val3:=4). But this does not work. It would be great if something like this would work! Thanks. Commented Jun 18, 2010 at 1:03
  • It has been awhile, looks like nobody can find a solution for this. Thanks for trying. Commented Jul 8, 2010 at 16:38

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.