Overloading Methods and More in VBScript - Implementing Optional Parameters
(Page 4 of 4 )
Many programming languages also offer the ability to add optional parameters to your functions. Full VB allows this by using the Optional keyword, but as with many features this was removed from the Scripting Edition. Here again, I have a cool little workaround that you may find useful.
Essentially, an optional parameter is one that may or may not be included when calling a function or subroutine. If omitted, this value will generally revert to a default value. Let’s begin with our original ping function again.
Function PingTest(strTarget, intTests)
arrResults = Array()
For i = 0 To intTests - 1
ReDim Preserve arrResults(i)
strComputer = "."
Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")
Set colPings = objWMIService.ExecQuery("Select * From Win32_PingStatus " _
& "Where Address = '" & strTarget & "'")
For Each objPing In colPings
If objPing.StatusCode = 0 Then
arrResults(i) = "Success"
Else
arrResults(i) = "Failure"
End If
Next
Next
PingTest = arrResults
End Function
Here, I’ve changed the PingTest function. It accepts a single string value for an IP address, but it also accepts an integer number of tests. The addition of a For loop allows me to perform the specified number of tests over and over against the same IP address. That is simple enough, but suppose that I don’t want to have to specify that integer value every time I call this function. If the value is omitted, I want the function to perform a single test; otherwise, perform the number provided.
Function PingTest(strTarget, intTests)
If IsNull(intTests) Then intTests = 1
arrResults = Array()
To accomplish this, I only need to insert a single line at the beginning of my function. This simply tests the intTests value, and if it is Null, it sets it to a default value.
arrResults = PingTest("127.0.0.1", Null)
For i = 0 To UBound(arrResults)
WScript.Echo arrResults(i)
Next
This is how to use the function. As you can see, the first parameter requires a string IP address. The second may contain an integer or a Null value. Then it’s just a matter of working through the array that is returned.
arrResults = PingTest("127.0.0.1", Null)
arrResults = PingTest("127.0.0.1", 3)
The function above can be called by either supplying a Null value or an actual integer. While not completely identical to an optional value, it does allow for some of the same usage.
As you can see, there are a few ways to improve your functions to regain some of the missing functionality that was removed from VBScript. I hope that you find these tips helpful. So, go ahead and experiment. See how these new ways of creating functions can improve your applications. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |