On ZDNet: Can Bing be your search engine?

How to omit or include optional arguments in VB6

Tags: Microsoft development tools, Peter Aitken, Microsoft Visual Basic 6.0, optional argument, Visual Basic Tips Newsletter

  • Save
  • Print
  • Recommend
  • 3

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!

  • Save
  • Print
  • Recommend
  • 3

Print/View all Posts Comments on this article

Use Multiple Methods Not Optional Arguments Wayne M. | 08/26/05
I'm Not Sure I Agree Completely WilRogJr@... | 08/26/05
Sorry, gotta disagree RobinHood70 | 08/26/05
Consider This Alternative Wayne M. | 08/26/05
That's my take too Tony Hopkinson | 08/27/05
Still disagree RobinHood70 | 08/29/05
Neither style is new Tony Hopkinson | 08/29/05
Agreed RobinHood70 | 08/31/05
If it;s simple and does that bulk of Tony Hopkinson | 08/31/05
They can be very handy Tony Hopkinson | 08/26/05

What do you think?

White Papers, Webcasts, and Downloads

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

Meet Doc

advertisement
Click Here