找回密码
 注册

QQ登录

只需一步,快速开始

查看: 260|回复: 0

Use Optional Parameters in VBScript

[复制链接]
发表于 2013-5-23 08:50:57 | 显示全部楼层 |阅读模式
本帖最后由 Test 于 2013-5-23 08:52 编辑

One of the nicest features of VisualBasic that is not supported in VBScript is theuse of optional arguments to a subroutine or function.  In fact, optional arguments are pretty standardin most high-level programming languages... so why aren't they available in VBScript?  I can't answer thewhy, 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 articlesthat discuss how to simulate optional arguments in VBScript.  In thearticle Optional Arguments in VBScript, author Francis Chau looks at twocommon methods to simulate optional arguments: an array based approach and a NULL value approach.  In thearray based approach, rather than having N arguments passed into a subroutine, the developer passes oneargument - 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 thearticle.)

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):
  1. function ComputeNodePathHtml(theNode, showImage = true, showLinks = true, forceVerdana = true, ...)
  2.   ...
  3. end function
复制代码
To use a class-as-function call, we first need to create a class.  This is a feature that VBScript startedsupporting 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 learnmore about creating classes with VBScript, be sure to read: Using Classeswithin 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!
  1. class NodePathHtmlGenerator
  2.         Dim theNode
  3.         Dim showImage
  4.         Dim showLinks
  5.         Dim forceVerdana

  6.         function RenderHtml
  7.                 if IsEmpty(showImage) then showImage = true
  8.                 if IsEmpty(showLinks) then showLinks = true
  9.                 if IsEmpty(forceVerdana) then forceVerdana = true
  10.                 ...
  11.         end function
  12. end class
复制代码
To call this function simply create an instance of the class, set whatever propertiesyou care to, and then call the main method:
  1. Dim node_path_obj
  2. Set node_path_obj = new NodePathHtmlGenerator
  3. Set node_path_obj.theNode = the_node
  4. Response.Write node_path_obj.RenderHtml
复制代码
Of course the above approach is three lines longer than it could be, but once you're commonly usingmore than a couple parameters (and not often using more than a few otherparameters), 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 theclass's properties as Private and then use Property Let and Property Getstatements to read and write the values, respectively.  Also note that you can have more than one function orsubroutine in the class.  Just be sure to create class properties for each of the optional parameters that eachfunction or subroutine may need.
Happy Programming!

By Michael Balloni

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 useoptional arguments.  Fortunately, JScript allows this use natively, so you don't have to use any tricks likewith VBScript.  However, the syntax for using optional arguments in JScript is a bit non-intuitive.  To learnmore, be sure to read this FAQ.


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-11 11:39 , Processed in 0.020861 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表