|
| Author |
Message |
ext237
Joined: 07 Jan 2008 Posts: 1
|
Posted: Mon Jan 07, 2008 3:27 pm Post subject: What is this function method called? |
|
|
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?
Archived from group: microsoft>public>vb>winapi |
|
| Back to top |
|
 |
Bob Butler
Joined: 04 Oct 2007 Posts: 1081
|
Posted: Mon Jan 07, 2008 1:39 pm Post subject: Re: What is this function method called? |
|
|
"ext237" wrote in message $0$293$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.
named parameters |
|
| Back to top |
|
 |
MikeD
Joined: 04 Oct 2007 Posts: 3348
|
Posted: Wed Jan 09, 2008 12:54 am Post subject: Re: What is this function method called? |
|
|
"ext237" wrote in message $0$293$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?
Named Parameters.
There's not really much to have to read or know about using named
parameters. When you call the function/sub/method, you just specify the name
of the parameter. You use ":=" and the actual name of the parameter. For
example:
Call functionName(Param3:=value, Param1:=value)
(I'm guessing you just didn't include the space between "optional" and
"Param" in your example function header and so the parameters names are
actually Param1, Param2, etc.).
Note that you can also pass the parameters in any order when all of them are
named in the call. If you name some parameters and not others in the call,
the unnamed parameters must be in the "proper" order. So, if you use named
parameters, you should name all of them that you're passing.
A few functions, subs, and methods of VB's objects and controls don't
support named parameters. For the most part, these are documented in VB's
Help. So, if you have problems using named parameters, be sure to check Help
to make sure it's supported.
I don't think I'd consider named parameters to be a shortcut though, since
using them will likely require more typing. It can, however, make your code
more readable for functions/subs/methods that have a lot of optional
parameters. That's about the only time I ever use named parameters. It's a
nice feature to have, but probably not something you'll really use much.
And just to be clear, a parameter being optional is not a requirement for
using named parameters. You can use named parameters even if all of them are
required. The only "advantages" would be somewhat better readability of code
(not significant since IntelliSense will provide the same info) and passing
the parameters in a different order. Neither of these is probably worth the
extra typing, IMO.
--
Mike
Microsoft MVP Visual Basic
|
|
| Back to top |
|
 |
|
|