c-- styles for logos and headline links do not modify internet, red, or black styles -->

Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!

VBScript in a Nutshell


 

VBScript in a Nutshell

By Paul Lomax, Matt Childs & Ron Petrusha


Chapter Excerpt: VBScript Program Structure

  • Introduction
  • Defining Subroutines: The Sub . . . End Sub Construct
  • Calling a Subroutine
  • Passing Variables into a Subroutine
  • Exiting a Routine with the Exit Statement
  • The Class Construct, Variables, Properties, Methods and Events
  • The Script Level: Active Server Pages
  • Windows Script Host
  • Client-Side Scripts for MSIE
  • Outlook Forms
  • Reusable Code Libraries: Active Server Pages
  • Reusable Code Libraries: Windows Script Host
  • Reusable Code Libraries: Client-Side Scripts for MSIE

    Printer Friendly Version

    Passing Variables into a Subroutine

    The ability to pass variables from one procedure to another is an important part of using custom procedures. It allows us to write custom "black box" routines that can behave differently depending on where the routine has been called from and also on the particular data values that the routine receives from the calling program.

    The data is passed from a calling routine to a subroutine by an argument list. The argument list is delimited with commas and can contain any data subtypes, including objects and arrays. For instance, the following mySubRoutine procedure expects three arguments: intDataIn1, strDataIn2, and lngDataIn3:

    Sub AnotherSubRoutine(  )
       some code. . . .
       mySubRoutine intvar1, strvar2, lngvar3
       more code that executes after mySubRoutine
    End Sub
     
    Sub mySubRoutine(intDataIn1, strDataIn2, lngDataIn3)
       code which uses incoming data
    End Sub
    

    When mySubRoutine is called from AnotherSubRoutine, it is passed three variables as arguments: intvar1, strvar2, and lngvar3. So as you can see, the names of variables passed in the calling routine's argument list do not need to match the names in the custom procedure's argument list. However, the number of variables in the two argument lists does need to match or a runtime error results.

    Passing Parameters by Reference

    If you're accustomed to programming in VB or VBA, you'll recognize the way that you pass arguments in VBScript. However, in Versions 1 and 2 of VBScript, this wasn't the case. Parameters could be passed only by value, and there was no support for passing parameters by reference.

    In addition, because VBScript is so flexible in its use of data types, you must take care when building subroutines that use data passed into them. The variables designated in the custom subroutine's argument list are automatically assigned the data types of the calling program's argument list. If a custom subroutine attempts to perform some inappropriate operation on the data passed to it, an error results, as the following code fragment illustrates:

    Sub AnotherSubRoutine(  )
       some code. . . 
       intVar1 = "Hello World"
       Call mySubRoutine (intvar1, strvar2, lngvar3)
       more code that executes after mySubRoutine
    End Sub
     
    Sub mySubRoutine(intDataIn1, strDataIn2, lngDataIn3)
       code that uses incoming data
       intResult = intDataIn1 * 10 'this will generate an error
    End Sub
    

    The custom subroutine mySubRoutine assumed that intDataIn1 would be an integer, but instead the calling program passed it a string variable, intVar1. Therefore VBScript automatically cast intData1 as a string. The subroutine then produces a runtime error when it attempts to perform multiplication on a non-numeric variable. As you can see, while loosely typed languages like VBScript have many advantages, one of their major drawbacks is the fact that you must be on your guard for rogue data at all times.

    Note that you can pass an argument to a procedure either by reference or by value. By default, arguments are passed by reference. By reference means that the calling routine passes the called function or subroutine a pointer to the argument (that is, its actual address in memory). As a result, any modifications made to the variable are reflected once control returns to the calling routine. The ASP code in Example 2-3 illustrates passing a variable by reference. The variable x is initially assigned a value of 10 in the DoSubroutine procedure. This value is then changed to 100 in the CallAnotherSub procedure. When control returns to the DoSubroutine procedure, the value of x remains 100 because the variable was passed by reference to CallAnotherSub.

    Example 2-3: Passing a Variable by Reference
    <SCRIPT LANGUAGE="VBScript" RUNAT="Server">
    Sub DoSubroutine(  )
       Dim x
       x = 10
       Response.Write "In DoSubroutine, x is " & x & "<P>"
       CallAnotherSub x
       Response.Write "Back in DoSubroutine, x is " & x & "<P>"
    End Sub
     
    Sub CallAnotherSub(ByRef var1)
       var1 = var1^2
       Response.Write "In CallAnotherSub, var1 is " & var1 & "<P>"
    End Sub
    </SCRIPT>
     
    About to call DoSubroutine <P>
    <%
       DoSubroutine
    %>
    

    Note that the Sub statement for CallAnotherSub explicitly indicates that its single parameter, var1, is to be passed by reference because of the ByRef keyword. Since this is the default method of passing parameters, however, the keyword could be been omitted. The statement:

    Sub CallAnotherSub(ByRef var1)
    

    is identical to:

    Sub CallAnotherSub(var1)
    

    On the other hand, by value means that the calling routine passes the called function or subroutine a copy of the variable. This means that any changes to the variable's value are lost when control returns to the calling program. The ASP code in Example 2-4 illustrates passing a variable by value. As was also true in Example 2-3, the variable x is initially assigned a value of 10 in the DoSubroutine procedure. This value is then changed to 100 in the CallAnotherSub procedure. When control returns to the DoSubroutine procedure, the value of x reverts back to 10 because the variable x was passed by value to CallAnotherSub.

    Example 2-4: Passing a variable by value
    <SCRIPT LANGUAGE="VBScript" RUNAT="Server">
    Sub DoSubroutine(  )
       Dim x
       x = 10
       Response.Write "In DoSubroutine, x is " & x & "<P>"
       CallAnotherSub x
       Response.Write "Back in DoSubroutine, x is " & x & "<P>"
    End Sub
     
    Sub CallAnotherSub(ByVal var1)
       var1 = var1^2
       Response.Write "In CallAnotherSub, var1 is " & var1 & "<P>"
    End Sub
    </SCRIPT>
     
    About to call DoSubroutine <P>
    <%
       DoSubroutine
    %>
    

    Note that the Sub statement for CallAnotherSub explicitly indicates that its single parameter, var1, is to be passed by value because of the ByVal keyword. This is necessary, since otherwise the variable would have been passed by reference.

    There is one additional way to pass an argument by value to a procedure that involves overriding the explicit or default ByRef keyword. For example, if we had called the CallAnotherSub procedure in Example 2-3 with the following syntax:

       CallAnotherSub(x)
    

    x would have been passed by value to the routine rather than by reference. The parentheses, in other words, cause an argument to be passed by value to a routine that is expecting to receive a parameter by reference. (Note that the converse does not work: parentheses do not cause an argument to be passed to reference to a routine that is expecting to receive a parameter by value.) This is a subtle difference that you should be aware of when passing parameters to procedures, since it can have unintended consequences.

    Printer Friendly Version

  • Introduction
  • Defining Subroutines: The Sub . . . End Sub Construct
  • Calling a Subroutine
  • Passing Variables into a Subroutine
  • Exiting a Routine with the Exit Statement
  • The Class Construct, Variables, Properties, Methods and Events
  • The Script Level: Active Server Pages
  • Windows Script Host
  • Client-Side Scripts for MSIE
  • Outlook Forms
  • Reusable Code Libraries: Active Server Pages
  • Reusable Code Libraries: Windows Script Host
  • Reusable Code Libraries: Client-Side Scripts for MSIE

  • Of Interest
    · The Elements of Intranet Style
    · A Tutorial in VBScript