Datasheet

Chapter 1: A Quick Introduction to Programming
3
It is not required that you declare all of the variables you use. By default, VBScript allows you to use
undeclared variables. However, it’s strongly recommended that you get into the good habit of declaring
all of the variables you use in your scripts. Declaring variables before use makes code easier to read and
to debug later. Just do it!
By declaring variables you also give them a name in the process. Here’s an example of a variable
declaration in VBScript.
Dim YourName
By doing this, you are in fact giving the computer an instruction to reserve some memory space for you
and to name that chunk
YourName . From now on, the computer (or, more accurately, the VBScript
engine) keeps track of that memory for you, and whenever you use the variable name
YourName , it will
know what you’re talking about.
Variables are essential to programming. Without them you have no way to hold all the data that your
script will be handling. Every input into the script, output from the script, and process within the script
uses variables. They are the computer’s equivalent of the sticky notes that you leave all over the place
with little bits of information on them. All the notes are important (otherwise why write them?) but they
are also temporary. Some might become permanent (so you take a phone number and write it down in
your address book or contact list), while others are thrown away after use (say, after reminding you to do
something). This is how it works with variables, too. Some hold data that you might later want to keep,
while others are just used for general housekeeping and are disposed of as soon as they’re used.
In VBScript, whenever you have a piece of information that you need to work with, you declare a vari-
able using the exact same syntax you saw a moment ago. At some point in your script, you’ll need to do
something with the memory space you’ve allocated yourself (otherwise, what would be the point of
declaring it?). And what you do with a variable is place a value in it. This is called initializing the vari-
able. Sometimes you initialize a variable with a default value. Other times, you might ask the user for
some information, and initialize the variable with whatever the user enters. Alternatively, you might
open a database and use a previously stored value to initialize the variable.
When we say database , we don’t necessarily mean an actual database but any store of data — it might
be an Internet browser cookie or a text file that we get the data from. If you are dealing with small
amounts of data a cookie or text file will suffice, but if you are dealing with a lot of data you need the
performance and structure that a database offers.
Initializing the variable gives you a starting point. After it has been initialized, you can begin making use
of the variable in your script.
Here’s a very simple VBScript example.
Dim YourName
‘ Above we dimensioned the variable
YourName = InputBox(“Hello! What is your name?”)
‘ Above we ask for the user’s name and initialize the variable
MsgBox “Hello “ & YourName & “! Pleased to meet you.”
‘ Above we display a greeting containing the user’s name
Rightly so, you’re now probably wondering what all this code means. Last time, you were showed one
line and now it’s grown to six.
c01.indd 3c01.indd 3 8/27/07 7:45:17 PM8/27/07 7:45:17 PM