Udostępnij za pośrednictwem


How to: Determine Whether an Optional Parameter Was Supplied

When a procedure defines an optional parameter, it might be necessary to determine whether or not the calling code has supplied the corresponding argument or omitted it.

If the parameter value compares equal to its default value, this could be for one of two possible reasons:

  • The calling code omitted the argument in the procedure call; or

  • The calling code supplied the argument with a value exactly equal to the parameter's default value.

The procedure code cannot distinguish between these two possibilities. Often it does not matter, but in some cases the procedure might need to take different actions for each possibility. The best available approach is to define an unlikely value as the default, although this does not guarantee that the calling code cannot supply it.

If it is important to be absolutely certain whether the calling program supplied an optional argument, the safest approach is to define overloaded versions of the procedure. See How to: Define Multiple Versions of a Procedure and Considerations in Overloading Procedures.

To determine whether an argument has been passed to an optional parameter

  1. Define an extremely unlikely value as the default for the parameter.

  2. If the optional parameter is a reference type such as a String, you can use Nothing as the default value, provided this is not an expected value for the argument.

  3. In the procedure code, compare the parameter against the default value and take the appropriate action.

Overloading a Procedure with and without an Optional Parameter

Another way to define a procedure with optional parameters is to use overloading. If you have one optional parameter, you can define two overloaded versions of the procedure, one accepting the parameter and one not accepting it. This approach becomes more complicated as the number of optional parameters increases. However, its advantage is that you can be absolutely sure whether the calling program supplied each optional argument.

To define different versions of the procedure to cover inclusion and omission of an argument

  1. Define one version of the procedure with the parameter in the argument list. Do not declare the parameter as Optional.

  2. Define another version of the procedure without the parameter. The declaration should be identical to that of the first version in every other respect.

  3. Place the code appropriate to each call in the respective version of the procedure.

Example

The following procedure defines the optional parameter office, and tests for its default value, QJZ, to see if it has been omitted in the call.

Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")
    If office = "QJZ" Then
        Debug.WriteLine("office not supplied -- using Headquarters")
        office = "Headquarters" 
    End If 
    ' Insert code to notify headquarters or specified office. 
End Sub

If the calling code does not supply a value for office in the argument list, Visual Basic supplies the default value of "QJZ".

If the optional parameter is a reference type such as a String, you can use Nothing (Visual Basic) as the default value, provided this is not an expected value for the argument.

For an example of using overloads to determine whether an optional parameter was passed, see How to: Overload a Procedure that Takes Optional Parameters.

See Also

Tasks

How to: Define Optional Parameters for a Procedure

How to: Call a Procedure that Takes Optional Parameters

Concepts

Procedure Parameters and Arguments

Passing Arguments by Value and by Reference

Passing Arguments by Position and by Name

Optional Parameters

Parameter Arrays

Procedure Overloading

Reference

Optional (Visual Basic)

ParamArray