|
|
|
|
|
|
|
|
Lesson 2-Working with VariablesA variable is a named location in computer memory that you can use for storage of data during the execution of your scripts. You can use variables to:
An Introduction to VariablesLet's look at a simple VBScript example to clarify the use of variables.
The first line of this example defines a sub procedure associated with the click event of a command button named cmdVariables. On the second line we declare a variable named Name. We are going to use this variable to store the name of the user when it is entered. The third line uses the InputBox function to first prompt for, and then return, the user's name. You will see more of the InputBox function later in this tutorial. The name it returns is stored in the Name variable. The fourth line uses the MsgBox function to display the user's name. Finally, the sub procedure completes on line five. Exactly how, and where, variables are stored is not important. What you use them for, and how you use them is important. That is what we will be looking at next. Declaring VariablesThere are two methods for declaring variables in VBScript, explicitly and implicitly. You usually declare variables explicitly with the Dim statement:
This statement declares the variable Name. You can also declare multiple variables on one line as shown below, although it is preferable to declare each variable separately:
Variables can be declared implicitly by simply using the variable name within your script. This practice is not recommended. It leads to code that is prone to errors and more difficult to debug. You can force VBScript to require all variables to be explicitly declared by including the statement Option Explicit at the start of every script. Any variable that is not explicitly declared will then generate an error. Variable Naming RulesWhen naming variables the following rules apply:
Variants and SubtypesVBScript has a single data type called a variant. Variants have the ability to store different types of data. The types of data that a variant can store are referred to as subtypes. The table below describes the subtypes supported by VBScript.
Assigning ValuesYou assign a value to a variable by using the following format:
The following examples demonstrate assigning values to variables:
Scope of VariablesThe scope of a variable dictates where it can be used in your script. A variable's scope is determined by where it is declared. If it is declared within a procedure, it is referred to as a procedure-level variable and can only be used within that procedure. If it is declared outside of any procedure, it is a script-level variable and can be used throughout the script. The example below demonstrates both script-level and procedure-level variables.
The variable counter is a script-level variable and can be utilized throughout the script. The variable temp exists only within the cmdButton_onClick sub-procedure. ConstantsVBScript does not provide support for constants, such as you find in other programming languages. You can work around this by assigning values to variables that you have defined as shown in the example below. Here, TAX_RATE is our constant.
ArraysThe VBScript language provides support for arrays. You declare an array using the Dim statement, just as you did with variables:
The statement above creates an array with 51 elements. Why 51? Because VBScript arrays are zero-based, meaning that the first array element is indexed 0 and the last is the number specified when declaring the array. You assign values to the elements of an array just as you would a variable, but with an additional reference (the index) to the element in which it will be stored:
Arrays can have multiple dimensions-VBScript supports up to 60. Declaring a two dimensional array for storing 51 states and their capitals could be done as follows:
To store values into this array you would then reference both dimensions.
VBScript also provides support for arrays whose size may need to change as the script is executing. These arrays are referred to as dynamic arrays. A dynamic array is declared without specifying the number of elements it will contain:
The ReDim statement is then used to change the size of the array from within the script:
There is no limit to the number of times an array can be re-dimensioned during the execution of a script. To preserve the contents of an array when you are re-dimensioning, use the Preserve keyword:
VBScript Tutorial
Contents· What is VBScript? · How to Use this Tutorial Lesson 1 · Adding VBScript to Web Pages · The <SCRIPT> Tag · Non-Supporting Browsers · Your 1stirst VBScript Exercise Lesson 2 · Working with Variables · Declaring Variables · Scope of Variables · Constants · Arrays · Your 2nd VBScript Exercise Lesson 3 · Objects and VBScript · Adding Objects to Web Pages · Linking VBScript with Objects · Your 3rd VBScript Exercise Lesson 4 · Controlling VBScript Routines · Conditional Statements · Looping Statements · Your 4th VBScript Exercise Lesson 5 · Using VBScript with Forms · Validating Your Forms · Your 5th VBScript Exercise · Summary
|
Intranet Journal's Tutorials |
|
Managing Editor |