Muokkaa

Jaa


Passing Arguments by Position and by Name (Visual Basic)

When you call a Sub or Function procedure, you can pass arguments by position — in the order in which they appear in the procedure's definition — or you can pass them by name, without regard to position.

When you pass an argument by name, you specify the argument's declared name followed by a colon and an equal sign (:=), followed by the argument value. You can supply named arguments in any order.

For example, the following Sub procedure takes three arguments:

Public Class StudentInfo
    Shared Sub Display(name As String,
                Optional age As Short = 0,
                Optional birth As Date = #1/1/2000#)

        Console.WriteLine($"Name = {name}; age = {age}; birth date = {birth:d}")
    End Sub
End Class

When you call this procedure, you can supply the arguments by position, by name, or by using a mixture of both.

Passing Arguments by Position

You can call the Display method with its arguments passed by position and delimited by commas, as shown in the following example:

StudentInfo.Display("Mary", 19, #9/21/1998#)

If you omit an optional argument in a positional argument list, you must hold its place with a comma. The following example calls the Display method without the age argument:

StudentInfo.Display("Mary",, #9/21/1998#)

Passing Arguments by Name

Alternatively, you can call Display with the arguments passed by name, also delimited by commas, as shown in the following example:

StudentInfo.Display(age:=19, birth:=#9/21/1998#, name:="Mary")

Passing arguments by name in this way is especially useful when you call a procedure that has more than one optional argument. If you supply arguments by name, you do not have to use consecutive commas to denote missing positional arguments. Passing arguments by name also makes it easier to keep track of which arguments you are passing and which ones you are omitting.

Mixing Arguments by Position and by Name

You can supply arguments both by position and by name in a single procedure call, as shown in the following example:

StudentInfo.Display("Mary", birth:=#9/21/1998#)

In the preceding example, no extra comma is necessary to hold the place of the omitted age argument, since birth is passed by name.

In versions of Visual Basic before 15.5, when you supply arguments by a mixture of position and name, the positional arguments must all come first. Once you supply an argument by name, any remaining arguments must all be passed by name. For example, the following call to the Display method displays compiler error BC30241: Named argument expected.

StudentInfo.Display("Mary", age:=19, #9/21/1998#)

Starting with Visual Basic 15.5, positional arguments can follow named arguments if the ending positional arguments are in the correct position. If compiled under Visual Basic 15.5, the previous call to the Display method compiles successfully and no longer generates compiler error BC30241.

This ability to mix and match named and positional arguments in any order is particularly useful when you want to use a named argument to make your code more readable. For example, the following Person class constructor requires two arguments of type Person, both of which can be Nothing.

Public Sub New(name As String, father As Person, mother As Person, dateOfBirth As Date)

Using mixed named and positional arguments helps to make the intent of the code clear when the value of the father and mother arguments is Nothing:

Dim p = New Person("Mary", father:=Nothing, mother:=Nothing, #9/21/1998#)

To follow positional arguments with named arguments, you must add the following element to your Visual Basic project (*.vbproj) file:

<PropertyGroup>
  <LangVersion>15.5</LangVersion>
</PropertyGroup>

For more information see setting the Visual Basic language version.

Restrictions on Supplying Arguments by Name

You cannot pass arguments by name to avoid entering required arguments. You can omit only the optional arguments.

You cannot pass a parameter array by name. This is because when you call the procedure, you supply an indefinite number of comma-separated arguments for the parameter array, and the compiler cannot associate more than one argument with a single name.

See also