Library tutorials & articles
Using Optional Parameters in VBScript
- Introduction
- The work-around
The work-around
|
|
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, ...)
|
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 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
|
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!
Related articles
Related discussion
-
Classic ASP : Page expires
by chezhian_in05 (0 replies)
-
how to select multiple files at a time using Ctrl+select and upload the attachments in C# .Net 1.0?
by vasanta (0 replies)
-
Header and Footer in Web page print
by fhajaj (4 replies)
-
help me to get simple requirement
by Slicksim (1 replies)
-
Gridview -> Template Field -> Button
by antti.simonen (1 replies)
Related podcasts
-
ASP.NET Caching and Performance
Steve Smith, owner of ASP Alliance and Lake Quincy Media joins us today to teach us about some hidden gems in ASP.NET caching and performance. Steve’s expertise in this area comes from first-hand experience as Lake Quincy’s ad system serves over 60 requests per second and handles over 150 million...
This thread is for discussions of Using Optional Parameters in VBScript.