You can specify the process parameters that are optional and do not need to provide them with variables when calling the process. In the process definition, "optional parameters" are defined by keywordsOptional. The following rules apply:
The default value must be specified for each optional parameter in the process definition.
The default value of an optional parameter must be a constant expression.
In the process definition, each parameter following the optional parameters must also be optional.
The following syntax shows the process declaration with optional parameters:
Code:
- Sub name (byval parameter 1 as Data Type 1, optional byval Parameter
Call the process with optional parameters
During running, the process cannot detect whether the specified parameter is omitted or whether the Call Code explicitly provides the default value. If you need to clarify this, you can set an impossible value as the default value. The following process defines the optional parameter office and tests its default value qjz to check whether it has been omitted in the call:
Code:
- Sub every Y (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 your y headquarters or specified office.
- End sub
If the optional parameter is an imageStringSuch a reference type can be used as long as it is not the expected value of the variable.NothingAs the default value.
Optional parameter and overload
Another way to define a process with optional parameters is to use overload. If there is an optional parameter, you can define two overloaded versions of the process. One accepts this parameter, and the other does not include this parameter. This method becomes more complex as the number of optional parameters increases. However, the advantage of this is that you can fully determine whether each optional parameter is provided by the calling program.