Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Using Parameters


    (Page 3 of 5 )

    Parameters are used to put data into a procedure.  Both Subs and Functions may use parameters in VBScript.  To understand this concept, however, you first need to understand variable scope.

    In VBScript, a variable’s life is determined by its scope at the time it is declared or initialized.  In other words, that variable reference is only valid within a certain scope.

    There are two scope levels in VBScript, script-level (sometimes referred to as “global”) and procedural.  In other words, a variable that is declared within a procedure is only valid within that procedure.  A script-level, or global, variable however is valid anywhere within the script.  To understand why this is important, let’s take a look at an example.

    strGlobal = "This variable is global."

     

    Sub MySub

       intProcedural = 5

    End Sub

     

    Function MyFunction

       Set objFso = CreateObject("Scripting.FileSystemObject")

    End Function

    In this example, the strGlobal variable is declared at the top-most level, or script-level.  This variable can be used anywhere throughout the script and will continue to be valid until it is either destroyed or script execution ends.

    The intProcedural and objFso variables are a little different.  Since they were created inside of procedures, they may only be used within those procedures.  Any attempt to use them outside will result in an error.  This is where parameters come into play.

    PrintMessage

     

    Sub PrintMessage

       intBorn = 1979

     

       WScript.Echo "Mary was born in", intBorn

       WScript.Echo "She is", CalculateAge, "years old."

    End Sub

     

    Function CalculateAge

       intAge = Year(Date) - intBorn

      

       CalculateAge = intAge

    End Function

    Here, I’ve taken our previous example and moved all of the Echo statements into a subroutine.  Now when I run this script is says that Mary is 2008 years old!  That’s not right.  What’s happening here?

    The problem is with the variable intBorn.  This variable only exists within the scope of the PrintMessage subroutine.  Therefore, it’s not available when the CalculateAge subroutine attempts to perform the age calculation so the year 2008 minus Nothing equals 2008!  To avoid this incorrect calculation, we need a way to put the intBorn variable into the scope of the CalculateAge function.

    PrintMessage

     

    Sub PrintMessage

       intBorn = 1979

     

       WScript.Echo "Mary was born in", intBorn

       WScript.Echo "She is", CalculateAge(intBorn), "years old."

    End Sub

     

    Function CalculateAge(intBorn)

       intAge = Year(Date) - intBorn

      

       CalculateAge = intAge

    End Function

    In order to make the intBorn variable available to the CalculateAge function, I have added a parameter.  Notice how I’ve changed the Function declaration.  When declaring a function or subroutine, you may include a list of comma-separated parameters in parentheses.  You must then provide a value for these parameters whenever you call the function or subroutine, as you can see in the Echo statement above.

    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 1 hosted by Hostway
    Stay green...Green IT