Optional Arguments

The Optional keyword can be used in the argument list of a procedure declaration to denote that a particular argument doesn't always have to be passed. This allows you the flexibility to have different calling procedures passing different argument lists. One restriction is that all arguments after the first optional argument must also be optional.

The Optional keyword was introduced in VB4, and at that time optional arguments could only be declared as type Variant. However, VB5 extended its functionality by allowing any intrinsic data type to be used as an optional argument.

To test if a variant optional argument has been passed into your procedure, use the IsMissing function. Other data types will have their default values if they have not been explicitly passed as arguments; this, however, may be confusing. If an optional integer value isn't passed as a parameter, for example, its value in the procedure is 0. But did the calling procedure actually pass in 0, or is its value 0 because the argument is missing?

Was this article helpful?

0 0

Post a comment