Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Using the Overloaded Constructor


    (Page 3 of 4 )

    Function PingTest(varTarget)

       If IsArray(varTarget) Then

           'Test against an array

           PingTest = arrResults

       Else

           'Test against a string

           PingTest = arrResults

       End If

    End Function

    You can see here that I’ve changed the logic of this function a bit.  Namely, I’m using the IsArray function along with a simple IF statement to determine exactly what type of data was passed into the function.  Now I can insert the code to handle each case.

    Function PingTest(varTarget)

       If IsArray(varTarget) Then

           arrResults = Array()

           For i = 0 To UBound(varTarget)

              ReDim Preserve arrResults(i)

              strComputer = "."

              Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")

              Set colPings = objWMIService.ExecQuery("Select * From Win32_PingStatus " _

                  & "Where Address = '" & varTarget(i) & "'")

              For Each objPing In colPings

                  If objPing.StatusCode = 0 Then

                     arrResults(i) = "Success"

                  Else

                     arrResults(i) = "Failure"

                  End If

              Next

           Next

           PingTest = arrResults

       Else

           arrResults = Array()

           ReDim Preserve arrResults(i)

           strComputer = "."

           Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")

           Set colPings = objWMIService.ExecQuery("Select * From Win32_PingStatus " _

              & "Where Address = '" & varTarget & "'")

           For Each objPing In colPings

              If objPing.StatusCode = 0 Then

                  arrResults(i) = "Success"

              Else

                  arrResults(i) = "Failure"

              End If

           Next

           PingTest = arrResults

       End If

    End Function

    Okay, this looks pretty complicated, but it’s really not as bad as it seems.  If you look closely, each branch of this IF block is identical to my original function, with one minor change in the If portion.  Here I’ve wrapped my function code inside of a For Each loop that moves through each IP address in the supplied array.  Each test’s result is added to the output array before it is returned by the function.  The Else portion remains the same as in the original function because it uses a string parameter as originally intended.

    Okay, so now that we’ve created an “overloaded” constructor, how do we use it in our scripts?  Well, let’s take a look.  First, let’s see it the way we wrote it the first time.

    arrResults = PingTest("127.0.0.1")

    WScript.Echo arrResults(0)

    This example performs a ping test against the local loopback address.  It should simply print “Success” on the screen.

    arrTests = Array("127.0.0.1", "192.168.0.0", "192.168.0.100")

    arrResults = PingTest(arrTests)

    For i = 0 To UBound(arrTests)

       WScript.Echo arrTests(i) & ": " & arrResults(i)

    Next

    Here I’m supplying an array of strings to test against.  The PingTest function still returns an array of results so it’s a simple matter of iterating through that array.  Each element in the results array matches the same element in the tests array.  Thus, the result for the test on arrTests(0) can be found in arrResults(0).

    More Windows Scripting Articles
    More By Nilpo


     

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek