VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

Example - Variable Scope in Procedures

This section provides a tutorial example on validating scope of global variables in the main code and local variables in procedures.

To help you understand variable scope concepts, I wrote the following sample code, function_variable_scope.html:

<html>
<body>
<!-- function_variable_scope.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   Dim vGlobalDim
   vGlobalDim = "Cat"
   vGlobalNoDim = "Dog"

   Dim vTempDim
   vTempDim = "Bird"
   vTempNoDim = "Fish"

   Call ScopeCheck()

   document.writeln("")
   document.writeln("Current value after Sub:")
   document.writeln("   vGlobalDim = " & vGlobalDim)
   document.writeln("   vGlobalNoDim = " & vGlobalNoDim)
   document.writeln("   vLocalDim = " & vLocalDim)
   document.writeln("   vLocalNoDim = " & vLocalNoDim)
   document.writeln("   vTempDim = " & vTempDim)
   document.writeln("   vTempNoDim = " & vTempNoDim)

Sub ScopeCheck()
   Dim vLocalDim
   vLocalDim = "Apple"
   vLocalNoDim = "Orange"

   Dim vTempDim
   vTempDim = "Banana"
   vTempNoDim = "Grape"
   
'  Updating values
   vGlobalDim = vGlobalDim & " - Updated by Sub"
   vGlobalNoDim = vGlobalNoDim & " - Updated by Sub"
   vLocalDim = vLocalDim & " - Updated by Sub"
   vLocalNoDim = vLocalNoDim & " - Updated by Sub"
   vTempDim = vTempDim & " - Updated by Sub"
   vTempNoDim = vTempNoDim & " - Updated by Sub"

'  Showing values
   document.writeln("")
   document.writeln("Current value in Sub:")
   document.writeln("   vGlobalDim = " & vGlobalDim)
   document.writeln("   vGlobalNoDim = " & vGlobalNoDim)
   document.writeln("   vLocalDim = " & vLocalDim)
   document.writeln("   vLocalNoDim = " & vLocalNoDim)
   document.writeln("   vTempDim = " & vTempDim)
   document.writeln("   vTempNoDim = " & vTempNoDim)
End Sub
</script>
</pre>
</body>
</html>

Here is the output:


Current value in Sub:
   vGlobalDim = Cat - Updated by Sub
   vGlobalNoDim = Dog - Updated by Sub
   vLocalDim = Apple - Updated by Sub
   vLocalNoDim = Orange - Updated by Sub
   vTempDim = Banana - Updated by Sub
   vTempNoDim = Grape - Updated by Sub

Current value after Sub:
   vGlobalDim = Cat - Updated by Sub
   vGlobalNoDim = Dog - Updated by Sub
   vLocalDim = 
   vLocalNoDim = 
   vTempDim = Bird
   vTempNoDim = Grape - Updated by Sub

Here are my comments about this example:

  • There are 6 variables in this example: vGlobalDim, vGlobalNoDim, vLocalDim, vLocalNoDim, vTempDim, and vTempNoDim.
  • The behavior of vGlobalDim and vGlobalNoDim is pretty consistent, defined in the main code; and accessible in the procedure. "Dim" or not makes no difference.
  • The behavior of vLocalDim and vLocalNoDim is also consistent, define in the procedure, not accessible in the main code. Notice that vLocalDim and vLocalNoDim are empty in the "after Sub" message.
  • The behavior of vTempDim and vTempNoDim shows that "Dim" statement forces vTempDim to a new local variable. So we have two variables of the same name, one in the main code, and one in the procedure. This is why vTempDim still has the old value after the subroutine call.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

"Function" and "Sub" Procedures

 What Is a Procedure?

 "Function" Statement and Function Call

 Function Procedure Example

 "Sub" Statement and Subroutine Call

 Sub (Subroutine) Procedure Example

 Passing Arguments to Procedures

 Example - Passing Arguments by Reference

 Example - Passing Arguments by Value

 Passing Arrays as Arguments

 Variable Scope in Procedures

Example - Variable Scope in Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

Example - Variable Scope in Procedures - Updated in 2015, by Dr. Herong Yang