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!
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Five Steps to Determine When to Virtualize YourServers VMware Thinking of virtualizing the servers at your company? Use this step-by-step guide to determine when's the best time to make your big move. Download Now
- Tom Davenport Study: Linking decisions and information for organizational performance IBM Tom Davenport's new client study looks at approaches to linking ... Download Now
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
- Building the Virtualized Enterprise with VMware Infrastructure VMware This paper explains how adopting a virtual infrastructure -- comprised of server, storage, and networking virtualization technologies -- can help your organization build a sustainable competitive ... Download Now
- Email Security and Archiving - Clearer in the Cloud Google The time is NOW for businesses and organizations of all sizes to implement ... Download Now
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

