Chapter 2. Program Structure

In order to write VBScript programs, you have to know how to structure your code so that your scripts and programs execute properly. Each of the different runtime environments for which you write VBScript code has different rules regarding program structure. We’ll look at each of these in turn. We’ll also examine the ways in which your host environment allows you to import VBScript code libraries, thus allowing you to create reusable code. Finally, we’ll end the chapter with a discussion of VBScript usage to write class modules. First, though, it’s important to cover the basic structures of VBScript that are relevant to all of the different script types: that global code calls code in individual functions or procedures.

Functions and Procedures

Functions and procedures (or subroutines) are central to modern programming. Dividing our script into subroutines helps us to maintain and write programs by segregating related code into smaller, manageable sections. It also helps to reduce the number of lines of code we have to write by allowing us to reuse the same subroutine or function many times in different situations and from different parts of the program. In this section, we’ll examine the different types of subroutines, how and why they are used, and how using subroutines helps to optimize code.

Defining Subroutines: The Sub . . . End Sub Construct

The Sub...End Sub construct is used to define a subroutine; that is, a procedure that performs some operation ...

Get VBScript in a Nutshell, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.