Opttypedparam




Public Sub SubX(Optional b As Boolean)
If IsMissing(b) Then
MsgBox "b is missing"
Else
MsgBox "b is not missing"
End If
End Sub

...
'Call SubX with no parameters

SubX

Public Sub SubX(Optional i As Integer = 1)
...
...
End Sub

Starting with Visual Basic 4.0, you could define optional
parameters. There was only one problem:
They could be only of type Variant. With VB 5.0, you can define
typed optional parameters. However, you must be careful when
doing so, because you can't check whether a typed optional
parameter was received. Consider this sample code:

You'd expect to see a message box indicating that b is missing,
but no box appears. The reason lies in the definition of IsMissing:
"Returns a Boolean value indicating whether an optional Variant
argument has been passed to a procedure.
" If you don't use a Variant argument, IsMissing won't provide the
expected value. A typed optional parameter is never missing;
it's always set to the default value for each type
(False for Boolean parameters, 0 for numbers and zero-length
strings). Another option is to add the default value in the
declaration of the procedure, as follows:












( opttypedparam.html )- by Paolo Puglisi - Modifica del 17/12/2023