To read the article online, visit http://www.4GuysFromRolla.com/webtech/100301-1.shtml

A Nifty Way to Use Optional Parameters in VBScript

By Michael Balloni


One of the nicest features of VisualBasic that is not supported in VBScript is the use of optional arguments to a subroutine or function. In fact, optional arguments are pretty standard in most high-level programming languages... so why aren't they available in VBScript? I can't answer the why, but I can propose a workaround.

If you're a regular 4Guys visitor like myself, you're likely aware that there are already a number of articles that discuss how to simulate optional arguments in VBScript. In the article Optional Arguments in VBScript, author Francis Chau looks at two common methods to simulate optional arguments: an array based approach and a NULL value approach. In the array based approach, rather than having N arguments passed into a subroutine, the developer passes one argument - namely an array containing the values for the specified parameters. In the NULL value approach, specified values are passed into the subroutine while NULL values are passed in for the non-specified values. (For more information on either of these techniques, be sure to read the article.)

Interested in Using Optional Parameters in JScript?
If you use JScript as your server-side scripting language, you may be wondering if (and how) you can use optional arguments. Fortunately, JScript allows this use natively, so you don't have to use any tricks like with VBScript. However, the syntax for using optional arguments in JScript is a bit non-intuitive. To learn more, be sure to read this FAQ.

One method for simulating optional arguments in VBScript that has not been discussed in any 4Guys article, though, is the use of the class-as-function-call method. This method is very popular with component developers. You could say that SoftArtisan's entire FileUp component is one class-as-function call. What I mean by this is instead of saying something like this (which VBScript won't even allow but VB and C++ do):

function ComputeNodePathHtml(theNode, showImage = true, showLinks = true, forceVerdana = true, ...)
  ...
end function

To use a class-as-function call, we first need to create a class. This is a feature that VBScript started supporting back in version 5.0. To learn what version of VBScript you are running, check out: Determining the Server-Side Scripting Language and Version. To learn more about creating classes with VBScript, be sure to read: Using Classes within VBScript.

To utilize the class-as-function method, our class should contain a member variable for each parameter. Each function and subroutine that supports optional parameters should accept zero parameters. While this may seem a tad confusing, check out the code below, which will (hopefully) clear everything up!

class NodePathHtmlGenerator
	Dim theNode
	Dim showImage
	Dim showLinks
	Dim forceVerdana

	function RenderHtml
		if IsEmpty(showImage) then showImage = true
		if IsEmpty(showLinks) then showLinks = true
		if IsEmpty(forceVerdana) then forceVerdana = true
		...
	end function
end class

To call this function simply create an instance of the class, set whatever properties you care to, and then call the main method:

Dim node_path_obj
Set node_path_obj = new NodePathHtmlGenerator
Set node_path_obj.theNode = the_node
Response.Write node_path_obj.RenderHtml

Of course the above approach is three lines longer than it could be, but once you're commonly using more than a couple parameters (and not often using more than a few other parameters), the clarity of named parameters outweighs the extra typing.

To ensure that the properties were of the correct data type and format, you'd likely want to create the class's properties as Private and then use Property Let and Property Get statements to read and write the values, respectively. Also note that you can have more than one function or subroutine in the class. Just be sure to create class properties for each of the optional parameters that each function or subroutine may need.

Happy Programming!

  • By Michael Balloni
    StreamLoad Developer...


  • Article Information
    Article Title: A Nifty Way to Use Optional Parameters in VBScript
    Article Author: Michael Balloni
    Published Date: Wednesday, October 03, 2001
    Article URL: http://www.4GuysFromRolla.com/webtech/100301-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers