Read an Excerpt
Chapter 1: A (Very) Short Introduction to Programming
In trying to squeeze the basics of writing computer programs into one chapter, we may be attempting the impossible, but we're going to do our best. The reason for including this chapter is that many people come to a scripting language, like VBScript, as their first language. Perhaps you're a network systems expert who wants to use VBScript and the Windows Script Host to write login scripts and automate administration tasks. Or perhaps you're a web designer who feels the need to branch out from the visual aspects of creating web pages and into writing scripts to drive content. Or perhaps you , re just a person who wants to learn a programming language for the fun of it. Either way, you've come to the right place.
Programming - or "writing code," as some people like to call it - is a huge subject. Many volumes have been written about it. During this chapter, in a single paragraph, we might introduce multiple unfamiliar concepts. We're going to move pretty fast, but if you read along closely and try out the examples, you'll probably be just fine.
Keep in mind that even an in-depth discussion of how to write computer programs might not even begin to touch on subjects such as architecture, systems design, database design, testing, documentation, and all the rest of the subjects that an accomplished software developer must master. But don't let all that discourage you. Everyone starts somewhere, and this is a great place for you to start learning the art and science of making a computer sing. Consider this chapter a brief introduction to the important building blocks. It won't make you an expert overnight, but hopefully it will give you a the know-how you'll need in order to get the most out of the rest of the book.
Variables and Data Types
In this section, we're going to be moving quickly through some of the most basic concepts of programming: variables, comments, using built-in VBScript functions, a other syntax issues.
The first concept we're going to touch on is that of variables. Simply put, a variable is a place in memory where your script holds a piece (or a set) of information (we'll use term data in place of "information" throughout most of this discussion). The data stored in a variable might be very simple, like the number 10,000, or very complex, such as a whole series of numbers, ranging from 1 to 10,000.
Behind the scenes, a variable is a reserved section of the computer's memory. just to make sure we're dear, memory is temporary working space for the computer. Memo is transient -that is, things that are placed in memory are not stored there permanently. That's what the hard drive is for. Since memory is transient, and since variables are stored in the computer's memory, variables are by extension transient a well. Your script will use variables to temporarily store a piece of information that the script needs to keep track of. (If your script needs to store that information permanently, it would store it in a file on the hard disk, or in a database, which is also stored permanently on the hard drive.)
In order to make it easier for the computer to keep track of all the millions of pieces 0 information that might be stored in memory at any given moment, memory is broke up into chunks. Each chunk is exactly the same size, and each chunk is given an address. You don't need to worry about memory addresses, but it's useful to know that a variable is a reserved set of one or more chunks. Different types of variables take up different amounts of memory. In your VBScript program, you will declare (or "dimension") variables before you use them, and you will give them a name in the process. Here's an example of a variable declaration in VBScript:
When you declare a variable like this, you are basically telling the computer "Reserve some memory for me, and please give that memory the name CustomerName." The computer (or, more precisely, the VBScript engine) keeps track of that memory for you and whenever you use the variable name CustomerName, it will know what you're talking about.
Note: it is not strictly required that you declare all of the variables you use. VBScript by default allows you to use undeclared variables. However, we strongly recommend that you declare all of the variables you use in your scripts. We'll cover this topic in more depth in Chapter 2.
Variables are essential to the activity of writing a VBScript program (or any program for that matter). Without variables, you'd have no way of keeping track of all of the pieces of information your script is going to be manipulating, adding up, and displaying on the screen. Picture yourself at your desk keeping track of your household income and expenses in a paper-based ledger.
This process entails adding up and keeping track of multiple pieces of information: paychecks and other kinds of income, grocery expenses, automobile expenses, medical expenses, debt service, and so on. You'd keep each of these running totals in a separate column, and later you'd probably add them all up to create a view into your financial situation. If you were writing a computer program to keep up with all this information instead of using your paper-based ledger, you would probably store the permanent data in a database, but while your program was accepting input of the numbers and keeping track of the totals, it would use different variables to keep up with each different piece of information.
In VBScript, whenever you have a piece of information that you need to work with, you would declare a variable using the syntax we demonstrated a moment ago. At some point in your script, you're going to need to place a value in that variable otherwise, what would be the point of declaring it? Placing a value in a variable for the first time is called initializing the variable. 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 types in. Or you might open a database and use a previously-stored value to initialize the variable.
Initializing the variable gives you a starting point. After its been initialized, you can use the variable in calculations, store it in a database, or display it back to the user in other form. Here's a simple VBScript example:
'Initialize the variable
DateToday = Date
MsgBox 'Today's date is DateToday
Now we've opened up a bit of a can of worms. What's all I that other stuff in this code? We'll look at it line-by-line. The first line is the variable declaration. We've asked the computer to reserve some memory for us, and to please remember the variable name DateToday for us.
All of the examples in this chapter are tailored so that they can be run by the Windows Script Host. The Windows Script Host is a scripting host that allows you to run VBScript programs within Windows. WSH will allow you to try these example programs out for yourself. You may already have WSH installed. To find out, type the above example script into a text editor, save the file as TEST. VBS, and double click the file in Windows Explorer. If the script runs, then you're all set. If Windows does not recognize the file, then you'll need to download and install WSH from http://msdn.microsoft.com/ scripting.
If you like, you car, skip ahead briefly and check out the beginning sections of Chapter 10. You don't need to read the whole chapter, just the first sections, which describe how to install the Windows Script Host, and how to use WSH to run scripts.
Let's get back to the code extract shown above. The second line is a comment In VBScript, any text that follows the single quote character ( ' ) is treated as a comment. This means that the VBScript engine will ignore this text. This introduces an interesting point: if the script engine is going to ignore this text, why type it in at all? It doesn't contribute to the execution of the script, right? This is correct, but it excludes one of most important principles of programming: it is equally important to write a script with human readers in mind as it is to write with the computer in mind.
Of course, when we are writing a script, we must write it with the computer (or, more specifically, the script engine) in mind, because if we don't type it in correctly (that is we don't use the correct syntax), the script engine won't execute the script. However, programming is an inherently human-involved activity. Once you've written some useful scripts, you're probably going to have to go back to make changes to a script you wrote six months ago. If you did not write that code with human readers in mini it might be difficult to figure out what in the world you were thinking at the time you wrote the script. Worse yet, what happens when one of your co-workers has to go in and make changes to a script you wrote six months ago? If you did not write that script to be readable and maintainable, your co-worker will probably curse you as they try to decipher every line of your code.
Adding good comments to your code is only one aspect of writing readable, maintainable programs. We'll touch on some other principles later, such as choosing good variable names, indenting properly, using white space in a helpful way, and organizing your code clearly and logically. That said, keep in mind that adding too many comments, or adding comments that are not useful, can make a script almost a bad as one with no comments at all, Also, if you are scripting for a web page that mu be downloaded to a user's browser, too many comments can affect the time that it takes to download the page.
We'll discuss some good commenting principles later in this chapter, but suffice it to say now that the comment in line two of our script is not really a good comment for everyday use. This is because, to any semi-experienced programmer, it is painfully obvious that what we are doing is initializing the DateToday variable. Throughout this book, you will often see the code commented this way. This is because the point our code is to instruct you, the reader, in how a particular aspect of VBScript programming works.
Back to our example script. It should now be clear that what we're doing in line three initializing the variable DateToday. To do this, we are using a built-in VBScript function called Date. A function is a piece of code that returns a value. VBScript comes with a lot of built-in functions. These functions are part of the language, and they're always available for your use. You can find a full listing of all of VBScript's built-in functions in Appendix A. The Date function is one such function that returns the current date according to the computer's internal clock. (In a few minutes, we'll get into the idea of writing your own functions.) In line three, we are telling the script engine, "Take the value returned by the Date function, and store it in the variable called DateToday." (Notice that we did not name the variable Date, but rather DateToday. This is necessary because, since Date is a built-in VBScript function, "date" is a reserved word. You cannot name your variables with the same name as a reserved word.)...