Datasheet

Chapter 1: A Quick Introduction to Programming
5
the YourName variable on the code line above. However, throughout this book you’ll often see the code
commented in a similar way. This is because the point of the code is to instruct the reader in how a par-
ticular aspect of VBScript programming works, and the best way to do that is to add comments to the
code directly. It removes ambiguity and keeps the code and comments together.
Also worth noting is that comments don’t have to be on a separate line. Comments can also follow the
code, like so:
Dim YourName ‘ initialize the variable
YourName = InputBox(“Hello! What is your name?”) ‘ ask for the user’s name
MsgBox “Hello “ & YourName & “! Pleased to meet you.” ‘ display a greeting
This works in theory but it isn’t as clear as keeping the comments on separate lines in the script.
Using Built-in VBS cript Functions
OK, back to the script. Take a look at line 3.
YourName = InputBox(“Hello! What is your name?”)
Here you are doing two things at once. First, you’re initializing the variable. You could do it directly,
like this:
YourName = “Fred”
However, the drawback with this is that you’re making the arbitrary decision that everyone is called
Fred , which is ideal for some applications but not for others. If you wanted to assign a fixed value to a
variable, such as a tax rate, this would be fine.
Dim TaxRate
TaxRate = 17.5
Because you want to do something that gives the user a choice, you should employ the use of a function,
called
InputBox . This function and all the others are discussed in later chapters, but for now all you
need to know is that
InputBox is used to display a message in a dialog box, and it waits for the user to
input text or click a button. The
InputBox generated is displayed in Figure 1-1 .
Figure 1-1
The clever bit is what happens to the text that the user types into the input box displayed — it is stored
in the variable
YourName .
c01.indd 5c01.indd 5 8/27/07 7:45:18 PM8/27/07 7:45:18 PM