How to omit or include optional arguments in VB6
Takeaway: 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!
SponsoredWhite Papers, Webcasts, and Downloads
- Top 10 Reasons Why TCP Is Reliable Global Knowledge
- The OSI Model: Understanding the Seven Layers of Computer Networks Global Knowledge
- SharePoint: How It's Leveraged and How It Works Global Knowledge
- The Case for Virtual Local Area Networks (VLANs) Global Knowledge
- Ten Ways Hackers Breach Security Global Knowledge
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

