Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Understanding Procedures 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Understanding Procedures in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-07-14

    Table of Contents:
  • Understanding Procedures in VBScript
  • Subs and Functions
  • Using Parameters
  • More on Scopes and Parameters
  • Final Concepts

  • 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


    Understanding Procedures in VBScript - More on Scopes and Parameters


    (Page 4 of 5 )

    In the last example, I passed the intBorn variable as a parameter which made it available in a new scope.  This is known as passing a variable “by reference.”

    When a script runs, the script host stores variables in system memory.  It creates a “reference” to the memory address where it stores the “value.”  The variable name that we assign is actually a pointer to that reference.

    When we pass that variable as a parameter, it is passed by reference, which means that the new scope will use the same memory reference.  In other words, if we were to change the variable's value in the new scope, those changes would also appear in the original scope as well.

    PrintMessage

     

    Sub PrintMessage

       strMessage = "        This is my message.       "

       WScript.Echo strMessage

       Call CleanString(strMessage)

       WScript.Echo strMessage

    End Sub

     

    Sub CleanString(strMessage)

       strMessage = Trim(strMessage)

    End Sub

    Take for instance this example.  It simply takes a string and removes any leading and trailing white spaces, and then displays the resulting string.  You can see when you run this script that the value of the strMessage variable is changed by the CleanString subroutine.  This is because the variable was passed “by reference.”

    To better understand this concept, let’s take a look at its converse.  The opposite of passing a variable by reference is passing a variable “by value.”  When passing a variable by value, the value is maintained while a new reference is created in memory.  This is used to preserve the original value of a variable.

    PrintMessage

     

    Sub PrintMessage

       strMessage = "        This is my message.       "

       WScript.Echo strMessage

       Call CleanString(strMessage)

       WScript.Echo strMessage

    End Sub

     

    Sub CleanString(ByVal strMessage)

       strMessage = Trim(strMessage)

       WScript.Echo strMessage

    End Sub

    Look again at my Sub statement.  Notice that I’ve included the ByVal keyword to instruct VBScript to pass strMessage by value instead.  This preserves the original value of strMessage in the PrintMessage scope by creating a duplicate reference in memory.  One is used in the PrintMessage scope and the other is used within the CleanString scope.  I’ve added some extra Echo statements so that you can see the value of the variables within each of the different scopes.

    There is also a ByRef keyword to specify passing a variable by reference.  However, it is unnecessary in VBScript since this is the default behavior.

    What about VBScript’s naming conventions?  You’ve been told that you cannot have two different variables with the same name, right?  In VBScript, two variables may have the same name as long as they are created and used in the context of different scopes.

    The second (and probably more common) use of parameters is to provide flexibility for your script by performing a set of predefined actions on different sets of data.

    Call PrintMessage("This is the first message.")

    Call PrintMessage("This is the second message.")

     

    Sub PrintMessage(strMessage)

       WScript.Echo strMessage

    End Sub

    In this example, I’m using parameters to provide different data to my subroutine each time it runs.

    More Windows Scripting Articles
    More By Nilpo


       · Procedures are an essential part of dynamic programming. Learn how these tools can...
       · Hi,I am learning VBScript and further ASP.I believe the VB scriptt eaching...
       · Thank you so much for your comments. I'm glad you enjoyed the article and I hope...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT