468,742 Members | 1,899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

How to Use Named Arguments

ADezii
8,800 Expert 8TB
When a call is made to a Sub or Function Procedure, you can supply Arguments in the exact order they appear in the Procedure's definition, or you can supply them in any position by name. To illustrate this point, I'll use a fictitious Function called fCalculateInterest() which accepts 3 Arguments (Currency, Single, and Long) and returns a value of type Currency.
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateInterest(curPrincipal As Currency, sngRate As Single, lngTermInMonths As Long) As Currency
  2.   fCalculateInterest = curPrincipal * sngRate * lngTermInMonths
  3. End Function
  4.  
A simple Call to this Function would be:
Expand|Select|Wrap|Line Numbers
  1. Dim curInterest As Currency
  2. curInterest = fCalculateInterest(100000,.0725, 240)
  3.  
The above Function was called by supplying its Arguments in the correct positions, each delimited by a comma. If the positions of the Arguments change, you may either get a Data Type mismatch Error, or erroneous results. By the use of Named Arguments, you can supply the Arguments by name without any regard to position. A Named Argument consists of an Argument name followed by by a Colon and an Equal Sign (:=). We will now call that very same Function but this time reversing the positions of Arguments:
Expand|Select|Wrap|Line Numbers
  1. Dim curInterest As Curremcy
  2. curInterest = fCalculateInterest(lngTermInMonths:=240, sngRate:=.0725, curPrincipal:=100000)
  3.  
Named Arguments are extremely useful when you are calling a Procedure that has Optional Arguments. If you use Named Arguments, you don't have to include commas to denote missing position Arguments, if you don't use them, you must. The following Sub Procedure will illustrate my point, It accepts 4 Arguments, 3 of which are Optional (String, Long, Boolean).
Expand|Select|Wrap|Line Numbers
  1. Private Sub TestArgs(Arg1 As Integer, Optional Arg2 As String, Optional Arg3 As Long, Optional Arg4 As Boolean)
  2. End Sub
  3.  
A typical call to this Sub Procedure would be as follows:
Expand|Select|Wrap|Line Numbers
  1. Call TestArgs(1234, "Argument2", 999999, False)
  2.  
If you wanted to call TestArgs without passing the 2nd and 3rd Optional Arguments, and without using Named Arguments, the syntax would be:
Expand|Select|Wrap|Line Numbers
  1. Call TestArgs(1234,,,False)
If you wanted to call TestArgs without passing the 2nd and 3rd Optional Arguments, and use Named Arguments, without regard to the positions of the Arguments, the syntax would be:
Expand|Select|Wrap|Line Numbers
  1. Call TestArgs(Arg4:=False, Arg1:=1234)
NOTE: As a more realistic and practical example, and as suggested by Mary, I've included an example using the popular SendObject() Method. All Arguments of this method were used, except [TemplateFile], and their standard positions within this Method were modified. This was only possible by using this weeks Tip, Named Arguments:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SendObject OutputFormat:=acFormatXLS, ObjectName:="tblEmployeePFD", _
  2. ObjectType:=acSendTable, Subject:="Named Arguments", Bcc:="Joe Dunn", _ 
  3. To:="Tom Jones", CC:="Chris Myers", EditMessage:=False, _ MessageText:="Demonstration of the Usage of Named Arguments"
  4.  
The morale of the story is: Is is very advantageous to use Named Arguments, especially when utilizing Optional Arguments.
Nov 5 '07 #1
2 11724
missinglinq
3,532 Expert 2GB
Most useful info, ADezii, for two reasons!

First, there's all those functions that have a gazillion arguments, while usually only requiring

1 gazillion - (1 gazillion - 3) of those arguments!

And secondly, while it may take a nanosecond or two longer to enter the code, you can tell at a glance what the utilized parameters were used for! Far less cryptic!

Linq ;0)>
Nov 12 '07 #2
ADezii
8,800 Expert 8TB
Most useful info, ADezii, for two reasons!

First, there's all those functions that have a gazillion arguments, while usually only requiring

1 gazillion - (1 gazillion - 3) of those arguments!

And secondly, while it may take a nanosecond or two longer to enter the code, you can tell at a glance what the utilized parameters were used for! Far less cryptic!

Linq ;0)>
Glad you like it Linq, one of my Favorites.
Nov 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Fuzzyman | last post: by
2 posts views Thread by Fuzzyman | last post: by
48 posts views Thread by Adam Ruth | last post: by
5 posts views Thread by Booted Cat | last post: by
17 posts views Thread by Ben R. | last post: by
3 posts views Thread by Adam Hartshorne | last post: by
36 posts views Thread by Pacific Fox | last post: by
2 posts views Thread by Ramashish Baranwal | last post: by
2 posts views Thread by fahad.usman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.