Function shortcut help

E

ext237

Hi,

I recently saw some VB code and there was a shortcut and I was hoping
someone knew what is its correct term.

When creating an object that required parameters (or a function), rather
than putting blanks or nulls in optional parameters, the code named the
param and gave it a value.

In other words, if the function was defined as

functionName(optionalParam1, optionalParam2, optionalParam3, optionalParam4)

it could be called as

functionName(optionalParamName3=paramValue3, optionalParamName4=paramValue4)

or something like that. I would like to read more about this, but what is
it called?
 
H

Helmut Weber

Hi,

IMHO objects don't have parameters.
They have properties and methods.
But that's not what you are asking about,
neither is is about a shortcut.

It seems to be that:

Public Function MyResult(x As String, Optional y As String)
MyResult = x & y
End Function

Sub Test9003()
MsgBox MyResult("Yes")
End Sub

See help on function statement:

"Optional" is a part of the argument list of a function

"Optional" itself is optional:

Indicates that an argument is not required. If used, all subsequent
arguments in arglist must also be optional and declared using the
Optional keyword. Optional can't be used for any argument if
ParamArray is used.

Try also:
Public Function MyResult(x As String, Optional y As String, _
Optional z As String)
MyResult = x & y
End Function

and:

Public Function MyResult(Optional x As String, y As String)
' no way!
MyResult = x & y
End Function

If an argument it optional,
following arguments must be optional as well.


HTH

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jay Freedman

Hi Helmut,

The poster (ext237) is so confused that he's confused you, too. ;-) I think
the question was not about optional arguments, either. It seems to be asking
about passing arguments by position versus passing them by name.

There's a topic in the VBA help about this, called "Understanding Named and
Optional Arguments". It begins with this:

"When you call a Sub or Function procedure, you can supply arguments
positionally, in the order they appear in the procedure's definition, or you
can supply the arguments by name without regard to position."

It then gives a number of examples.

One thing the original post had wrong was using a plain equal sign in the
call using named arguments. VBA requires the := operator in named argument
assignments.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
T

Tony Jollans

There are two ways to specify parameters to procedures: by position or by
name.

When specified by position, parameters must be in order and only trailing
optional ones can be left out although others may be (explicitly) empty.

When specified by name, the parameters can be in any order, and any optional
parameters can be ignored.

You can also mix the two methods by specifying the first 2 or 3 or 10 by
position followed by any or all of the remaining ones in any order.
 
E

ext237

Helmut, and Tony:

This is exactly what I was looking for. Sorry for the confusing post, at
the time I was trying to explain the syntax to a fellow Java developer (we
couldn't find an example on google) so my question was very rushed. Thanks
for the quick repleis!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top