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?

Re: Function shortcut help by Helmut

Helmut
Mon Jan 07 08:45:55 PST 2008

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

Re: Function shortcut help by Jay

Jay
Mon Jan 07 09:42:09 PST 2008

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Helmut Weber wrote:
> 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



Re: Function shortcut help by Tony

Tony
Mon Jan 07 09:51:08 PST 2008

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.

--
Enjoy,
Tony

"ext237" <ext237@somewhere.com> wrote in message
news:478251f7$0$345$bb4e3ad8@newscene.com...
> 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?
>


Re: Function shortcut help by ext237

ext237
Mon Jan 07 12:29:02 PST 2008

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!



"Jay Freedman" <jay.freedman@verizon.net> wrote in message
news:%23uUKiSVUIHA.5836@TK2MSFTNGP04.phx.gbl...
> 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 FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup
> so all may benefit.
>
> Helmut Weber wrote:
>> 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
>
>