Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Overloading Methods and More in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Overloading Methods and More in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2009-02-23

    Table of Contents:
  • Overloading Methods and More in VBScript
  • Information Overload
  • Using the Overloaded Constructor
  • Implementing Optional Parameters

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek