Software Development optimize

How to omit or include optional arguments in VB6

There may be times when you may want to define one or more optional arguments that can be included or omitted when a procedure is called. Read this VB6 tip to learn how you can do so with the Optional keyword.

Most VB6 procedures—subs and functions—take a fixed number of arguments. The arguments and their types are specified in the procedure definition and, when the procedure is called, the arguments that are passed must precisely match the definition. In some situations, you may want to define one or more optional arguments that can be included or omitted when a procedure is called. You can do so with the Optional keyword.

A procedure can have one or more optional arguments. You must place the optional arguments at the end of the argument list after any non-optional arguments. For example:

Sub foo(A As Integer, B As String, Optional C As Single, Optional D As Date)

End Sub

You can also specify a default value for an optional argument that will be used if the argument is not passed, like this:

Sub goo(Optional rate As Single = .05)

End Sub

If the argument rate is passed to the procedure, VB will use the passed value; if it's omitted, VB will use the value .05. If an optional argument is omitted and does not have a default value specified, the VB value for declared but uninitialized variables for that type is used, such as 0 for numeric variables.

Note: You cannot use optional arguments in procedures that use ParamArray arguments.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

0 comments