Variables in VBScript behave just like those variables you would use in Visual
Basic. If you don't know Visual Basic don't worry because we are going to put you
through Tizag's Five Minute VBScript Variable Bootcamp.
For those of you who have not used variables before, then you
should know that the notion of variables helps to make programs
easier to program and to understand. Variables are used to hold information so that
it can be reused throughout the program. You can think of variables as a
grocery bag and your information you want to store as your groceries. Without
the bag it's going to be a pain the bum to drag all that food around!
Declaring Variables in VBScript
When you are going to use a variable in your VBScript code you should
first declare it. To declare a variable in VBScript you use the Visual Basic
Nothing is displayed because we were simply setting up our variables for use!
Note: It is also possible to declare multiple variables in one line. Use
the comma "," to separate your multiple variable names.
Dim myVariable1, myVariable2
VBScript Variables: Setting Values
When you want to set your variable equal to a value you use the equals
character "=", also known as the equals operator. The name of your variable
appears on the left of the equals, while the value you are setting your variable equal
to appears to the right of the equals.
To use your variable, simply type the variable name where you would like to use it. In this
example we create two variables, set set one equal to a number and the other equal to a string.
We then use the document.write method to print both out to the browser.
Dim myVariable1, myVariable2
myVariable1 = 22
myVariable2 = "Howdy"
document.write("My number is " & myVariable1)
document.write("<br />My string is " & myVariable2)
My number is 22
My string is Howdy
VBScript Variables: Setting Objects
Setting Objects? Yes, VBScript allows you to use objects in your code. Objects
are blobs of code goo that can contain multiple methods and variable. When you want
to use these methods and variables you have to use the period operator "." to gain access to them. See
our VBScript Syntax lesson for more information.
When you are setting a value to a variable that is actually an object then you
must follow some special syntax. The SET keyword lets VBScript know that
you are setting your variable equal to an object. In addition to this, you also
have to set the variable equal to nothing after you are finished with it!
It's a lot of extra work, but it is necessary.
This example shows how to use a fictional class myClass to create an object. For this example
to work you would have to create myClass yourself. Going into more detail is behind
the scope of this lesson.
SET myFirstObject = New myClass
myFirstObject = nothing
Note: Once again, nothing is displayed because we were just showing you how
to set and release an object in VBScript!
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!