please dont rip this site
Microsoft® Visual Basic® Scripting Edition
VBScript Variables
Using VBScript |
Previous | Next |

 

What Is a Variable?
A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the time your script is running. For example, you might create a variable called ClickCount to store the number of times a user clicked an object on a particular Web page. Where the variable is located in computer memory is unimportant. What's important is that you only have to refer to it by name to see its value or to change its value. In VBScript, variables are always of one fundamental data type, Variant.

Declaring Variables
You declare variables explicitly in your script using the Dim statement, the Public statement, and the Private statement. For example:


 Dim DegreesFahrenheit

You declare multiple variables by separating each variable name with a comma. For example:


 Dim Top, Bottom, Left, Right

You can also declare a variable implicitly by simply using its name somewhere in your script. That's not generally considered to be a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in your script.

Naming Restrictions
Variable names follow the standard rules for naming anything in VBScript. A variable name:

Scope and Lifetime of Variables
When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable; it has local scope and is known as a procedure-level variable. Sometimes, however, you need to use a variable with a broader scope, such as one whose value is available to all the procedures within the same script. If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This kind of script-level variable is said to have script-level scope.

The length of time a variable exists is its lifetime. A script-level variable's lifetime extends from the time it is declared until the time the script is finished running. A local variable's lifetime begins when its declaration statement is encountered as the procedure begins, and ends when the procedure concludes. Local variables are ideal as temporary storage space when a procedure is executing. You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.

A variable's scope is determined by where you declare it. At script level, the lifetime of a variable is always the same. It exists for as long as the script is running. At procedure level, a variable exists only so long as you are in the procedure. When the procedure exits, the variable is destroyed.

Assigning Values to Variables
Values are assigned to variables creating an expression as follows: the variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example:


 B = 200

Scalars and Arrays
Much of the time, you just want to assign a single value to a variable you've declared. A variable containing a single value is a scalar variable. Other times, it's convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. Array variables are declared much the same way as scalar variables. The difference is that a declaration of an array variable uses parentheses following the variable name. In the following example, a single-dimension array containing 11 elements is declared:


 Dim A(10)

Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of an array is called a fixed-size array.

You assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 10, data can be assigned to the elements of an array as follows:


 A(0) = 256
 A(1) = 324
 A(2) = 100
 . . .
 A(10) = 55

Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example:


 . . . 
 SomeVariable = A(8)  
 . . . 

Arrays aren't limited to a single dimension. You can have as many of 60 dimensions although most people can't comprehend more than about three or four. Multiple dimensions are declared by separating an array's size numbers in the parentheses with commas. In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:


 Dim MyTable(5, 10)

In a two-dimensional array, the first number is always the number of rows; the second the number of columns.

You can also declare an array whose size changes during the time your script is running. This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement as with any other array, or using the ReDim statement. The difference is that no size or number of dimensions is placed inside the parentheses. For example:


 Dim MyArray()
 ReDim AnotherArray()

To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each. In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.


 ReDim MyArray(25)
 . . . 
 ReDim Preserve MyArray(30)

There is no limit to the number of times you can resize a dynamic array, but you should know that if you make an array smaller than it was, you lose the data in the eliminated elements.


© 1996 by Microsoft Corporation.

Comments:


file: /Techref/language/asp/VBS/VBSCRIPT/7.htm, 8KB, , updated: 2008/8/14 17:00, local time: 2024/11/24 09:52,
TOP NEW HELP FIND: 
3.145.72.44:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/techref/language/asp/VBS/VBSCRIPT/7.htm"> Variables - Microsoft&#174; Visual Basic&#174; Scripting Edition</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .