Programming languages come in two flavors when it comes to how data types are handled.
A
strongly typed
language forces the programmer to
declare, in advance, the data type of every variable so that the
compiler will know exactly what kind of value to expect in that
variable. If the programmer declares a variable with a numeric data
type, the compiler expects that variable to hold a number, and
produces an error if the programmer
In a loosely typed language, the programmer does not have to declare in advance the data type of a variable. Usually, in fact, a loosely typed language does not even provide a way to declare the data type. Scripting languages like VBScript are very often loosely typed. They use a generic, 'universal' super data type that can hold any type of data.
The
Write the code in plain text
Compile the code to produce an executable file
Run the compiled executable file
The program runs
Instead of a compiler, most scripting languages, including VBScript, have the concept of a runtime engine , which 'interprets' the code 'at runtime' instead of compiling it in advance. The process for a scripting language goes a bit differently:
Write the code in plain text
Execute the script file
The scripting runtime engine compiles the code 'on the fly'
The program runs
The delayed compilation that comes with a scripting language
goes hand-in-hand with the loose typing of the language. This is an
oversimplification, but since code is compiled on the fly, the
compiler can examine the data being placed into a variable and what
kinds of operations are being performed on the variable to
The concepts of loose typing, a universal data type, and
educated guessing about data types at runtime lead to some
interesting scenarios and behaviors when you execute the VBScript
code you have written. Throughout this chapter, we will closely
examine these details to ensure that you will not fall into any
programming traps
Let's consider for a moment the Visual Basic programmer's
perspective on data types. It may seem odd to suddenly change the
subject to a different programming language, but VBScript and
Visual Basic are actually very closely
However, the reason for bringing up Visual Basic is that, in a
discussion of data types, the concepts are simpler to explain and
easier to grasp when presented in the context of Visual Basic.
What's more, these concepts translate directly when we return in
the
You will remember from the previous section that Visual Basic is a strongly typed language, which means that a Visual Basic programmer must declare a specific data type for each variable used in his or her program. For example, here is a variable declaration in Visual Basic. What this line of code means is that the programmer is telling the computer to reserve space in memory for a variable called OrderTotal and that the data type that will be stored in that variable is the Currency data type. (The Currency type is used to store numeric values that represent an amount of money.)
Dim OrderTotal As Currency
By declaring the
OrderTotal
variable with the
Currency
data type, the programmer is signaling his or
her
OrderTotal = 695.95 OrderTotal = "Bill's Hardware Store"
The first line of code works fine, because a numeric value is being stored in the variable. However, the second line of code will produce an error because the type of data going into the variable does not match the declared data type. A strongly typed language also makes a line of code like the following produce an error.
OrderTotal = 695.95 + "Bill's Hardware Store"
This strongly typed syntax produces several technical benefits
in the compilation and performance of a Visual Basic application.
However, since this book is about VBScript, we're not going to get
into that. What we do want to talk about though are benefits that
translate directly to VBScript-namely, the predictability and
clarity that strong typing
A programmer always wants to accomplish at least two things:
fulfilling the requirements for the program (in other words,
building a program that will do what it is supposed to do) and
producing a program that is free of bugs and mistakes. Code that is
clear, readable, understandable, and predictable will always be
easier for human beings to read, understand, and change. Code that
is easy to read, understand, and change is always more likely to
fulfill the requirements and more likely to be free of
A Visual Basic programmer must declare a variable for a specific
purpose, give the variable a specific name, and declare the
intention to store only a specific type of data in that variable.
If all of the elements of a program are this neatly segmented,
given good specific
Things are a little different, though, for the VBScript
programmer. VBScript does not have any syntax for declaring a
variable with the
Currency
data type, or any other
specific data type. All VBScript
Dim OrderTotal
The syntax is almost the same, but VBScript does not support the As keyword for declaring a data type. This means that the VBScript programmer is free to put any kind of data in this variable he or she wants. The following two lines of VBScript code are both equally valid in VBScript. Unlike in Visual Basic, the second line of code will not produce an error.
OrderTotal = 695.95 OrderTotal = "Bill's Hardware Store"
Believe it or not, the second line of code that seems so ridiculous does not produce an error in VBScript. As mentioned a moment ago, in Visual Basic this line definitely produces an error when OrderTotal is declared with the Currency data type. However, in VBScript this line of code results in the value "695.95Bill's Hardware Store" stored in the OrderTotal variable.
OrderTotal = 695.95 + "Bill's Hardware Store"
The reason for these seemingly
First, we must understand how the
Variant
data type
works-in particular, how the
Variant
subtypes
Second, when we program in VBScript, we must