Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VBScript variable


May 13, 2021 VBScript


Table of contents


VBScript variable


Variables are easy-to-use placeholders and are "containers" for storing information.


VBScript variable

Try it - instance (IE only)

Create and change variables
How to create a variable, assign it a value, and then change its value.

Insert a variable value in a piece of text
How to insert a variable value in a piece of text.

Create an array
Arrays are used to store a series of related data items. This example shows how to create an array that stores names.


Remember the algeology you learned at school?

Remember the algeology you learned at school? x=5,y=6,z=x+y

Remember that? A letter (e.g. x) can hold a value (e.g. 5) and you can use the information above to calculate that the value of z is 11.

These letters are called variables, which can be used to hold values (x-5) or expressions (z-x-y).


VBScript variable

VBScript variables are used to hold values or expressions compared to algegege.

A variable can have a short name, such as x, or a more descriptive name, such as carname.

Rules for VBScript variable names:

  • Must begin with a letter
  • Cannot contain dots (.)
  • Cannot exceed 255 characters
  • Must be unique within the declared scope

In VBScript, all variables are related to type variant and can store different types of data.


Declare (create) vBScript variables

Creating a variable in VBScript usually refers to a "declaration" variable.

You can declare vBScript variables through Dim, Public, or Private statements. Here's what it looks like:

Dim x
Dim carname

Now you have created two variables. The names of the variables are "x" and "carname".

You can also declare a variable in a script by using its name. Here's what it looks like:

carname="Volvo"

Now you've created another variable. T he name of the variable is "carname". Then, this is not a good practice, because you might misspelled the variable name in the script, which might cause strange results while the script is running.

If you misspelled the variable name, such as the "carname" variable misspelled as "carnime," the script automatically creates a new variable named "carnime." T o prevent scripts from doing this, you can use Option Explicit statements. If you use this statement, you must use the dim, public, or private statement to declare all variables.

Place the Option Explicit statement at the top of the script as follows:

Option Explicit
Dim carname
carname=some value


Assign a value to a variable

You can assign a variable a value as follows:

carname="Volvo"
x=10

The variable name is on the left side of the expression, and the value that needs to be assigned to the variable is to the right of the expression. The value of the variable "carname" is now "Volvo" and the value of the variable "x" is "10".


The lifetime of the variable

The lifetime of a variable refers to the length of time it can exist.

When you declare a variable in a sub-program, the variable can only be accessed within that program. W hen you exit this program, the variable also expires. S uch variables are called local variables. You can use local variables with the same name in different sub-programs because each variable can only be recognized within the program that declared it.

If you declare a variable outside of a sub-program, it is accessible to all sub-programs on your page. T he lifetime of such variables begins when they are declared and ends when the page is closed.


VBScript array variable

Array variables are used to store multiple values in a single variable.

In the following example, an array of 3 elements is declared:

Dim names(2)

The number shown in parentheses is 2. T he lower sign of the array starts at 0, so the array contains three elements. T his is an array with a fixed capacity. You can assign data to each element of the array, as follows:

names(0)="Tove"
names(1)="Jani"
names(2)="Stale"

Similarly, by using the underseal of a particular array element, you can get back the value of any element. Here's what it looks like:

mother=names(0)

You can use up to 60 dimensionals in an array. T he way to declare a multi-dimensional array is to separate numbers with commas in parentheses. H ere, we declare a 2-dimensional array of 5 rows and 7 columns:

Dim table(4,6)

Assign a value to a two-digit group:

Instances (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 out . . .
We learned about VBScript variables, and in the next section, let's learn about the use of VBScript programs!