Named Arguments

This is the assignment of the parameter to the correct value. This is prefered since you don't need to include spaces for missing arguments and can place the arguments in any order
Used named arguments for methods that have lots of optional arguments and also when you only need to use a few.


There are some advantages to specifying paramaters by name
We can enter the arguments in any order and no not need to include extra unnecessary commas between the arguments we are not passing in.
We need to use := instead of just =.
Also the code is better documented and easier to understand.
Improves the readability and clarity of your code.


If you have a lot of optional arguments the procedure call can get very messing
Note the special syntac for named arguments, and the the colon before the equal sign.
Using named arguments remove the need for spaces for missing arguments.
You can also place the arguments in any order.


Some procedures especially some of the built-in methods can often contain a large number of paremeters where in fact all these parameters are declared as optional.
For example Excel's Workbook.SaveAs method.

ActiveWorkbook.SaveAs(FileName As String) 


Positional Arguments

The normal way to call a procedure and pass arguments is to pass in the arguments you want and leave the rest blank
It is the position of the argument that tells it what parameter it is.
This method will mean you have empty arguments to those you do not want
For example

ActiveWorkbook.SaveAs "C:\Temp.xls", , , , , True, , , True 

or

Call ActiveWorkbook.SaveAs("C:\Temp.xls", , , , , True, , , True) 

This is not very user friendly and also wastes lots of space.
An alternative is to use Named Arguments.


Using Named Arguments

This method allows you to avoid parameters you are not interested in.
You must declare the name of the argument followed by a colon and an equal sign.

ActiveWorkbook.SaveAs FileName:="C:\Temp.xls", _ 
                                     CreateBackup:=True, _
                                      AddToMru:=True


Important

Using Named Arguments is strongly recommended.


© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext