VBScript Variables
Variables are "containers" for storing information.
 |
Try it Yourself - Examples (IE Only) |
Create and change a variable
How to create a variable, assign a value to it, and then change the value of it.
Insert a variable value in a text
How to insert a variable value in a text.
Create an array
Arrays are used to store a series of related data items. This example demonstrates how you can make an array that stores names.
Do You Remember Algebra from School?
Do you remember algebra from school? x=5, y=6, z=x+y
Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11?
These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).
VBScript Variables
As with algebra, VBScript variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carname.
Rules for VBScript variable names:
- Must begin with a letter
- Cannot contain a period (.)
- Cannot exceed 255 characters
In VBScript, all variables are of type variant, that can store different types of data.
Declaring (Creating) VBScript Variables
Creating variables in VBScript is most often referred to as "declaring" variables.
You can declare VBScript variables with the Dim, Public or the Private statement. Like this:
Now you have created two variables. The name of the variables are "x" and "carname".
You can also declare variables by using its name in a script. Like this:
Now you have also created a variable. The name of the variable is "carname". However, this method is not a good practice, because you can misspell the variable name later
in your script, and that can cause strange results when your script is running.
If you misspell for example the "carname" variable to "carnime",
the script will automatically create a new variable called "carnime". To prevent your script from doing this, you can use the Option Explicit statement.
This statement forces you to declare all your variables with the dim, public or
private statement.
Put the Option Explicit statement on the top of your script. Like this:
Option Explicit
Dim carname
carname=some value |
Assigning Values to Variables
You assign a value to a variable like this:
The variable name is on the left side of the expression and the value you want to
assign to the variable is on the right. Now the variable "carname" has
the value of "Volvo", and the variable "x" has the value of "10".
Lifetime of Variables
How long a variable exists is its lifetime.
When you declare a variable within a procedure, the variable can only be
accessed within that procedure. When the procedure exits, the variable is destroyed.
These variables are called local variables.
You can have local variables with the same name in different procedures, because
each is recognized only by the procedure in which it is declared.
If you declare a variable outside a procedure, all the procedures on your page
can access it. The lifetime of these variables starts when they are declared,
and ends when the page is closed.
VBScript Array Variables
An array variable is used to store multiple values in a single variable.
In the following example, an array containing
3 elements is declared:
The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array.
You assign data to each of the elements of the array like this:
names(0)="Tove"
names(1)="Jani"
names(2)="Stale"
|
Similarly, the data can be retrieved from any element using the index of the particular array element you want.
Like this:
You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating
the numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and 7 columns:
Asign data to a two-dimensional array:
Example (IE Only)
<html>
<body>
<script type="text/vbscript">
Dim x(2,2)
x(0,0)="Volvo"
x(0,1)="BMW"
x(0,2)="Ford"
x(1,0)="Apple"
x(1,1)="Orange"
x(1,2)="Banana"
x(2,0)="Coke"
x(2,1)="Pepsi"
x(2,2)="Sprite"
for i=0 to 2
document.write("<p>")
for j=0 to 2
document.write(x(i,j) & "<br />")
next
document.write("</p>")
next
</script>
</body>
</html> |
Try it yourself »
|

Whether you're new to XML or already an advanced user, the user-friendly views and powerful entry helpers, wizards, and debuggers in XMLSpy are designed to meet your XML and Web development needs from start to finish.
New features in Version 2010!
- XML editor
- Graphical XML Schema / DTD editors
- XSLT 1.0/2.0 editor, debugger, profiler
- XQuery editor, debugger, profiler
- XBRL validator, taxonomy editor, taxonomy wizard
- Support for Office Open XML (OOXML)
- Graphical WSDL 1.1/2.0 editor & SOAP debugger
- JSON editing & conversion
- Java, C#, C++ code generation
- 32-bit and 64-bit versions
- And much more!
Download a free trial today!
|
|
|
|