Operators and statements are basic scripting tools that let you combine variables and constants to produce new data. This month, I introduce you to the four families of VBScript operators: arithmetic, comparison, logical, and concatenation. Next month, I'll cover statements.
As Table 1 shows, VBScript's arithmetic operators consist of the addition (+), subtraction or negation (-), multiplication (*), division (/), Integer division (\), exponentiation (^), and modulus (Mod) operators. All these operators are binary operators, which means they take two operands. However, when you use the - operator for negation, it is a unary operator (i.e., it takes only one operand).
Scriptwriters often use the + and - operators. You can use the + operator to work with numbers (e.g., Integers, Double values, Single values) and other data subtypes. (If you're unfamiliar with VBScript subtypes, see my column in the June issue.) For example, you can add two numbers represented by variables a and b and assign the result to the third variable x:
Or, you can use the + operator to concatenate (i.e., join in order) two strings. When you concatenate two strings, you append the second string to the end of the first string. For example, the code
str2 = "world"
MsgBox str1 + str2
returns the string "Hello world".
Typically, you use the + operator to add two variables or constants of the same subtype. However, you can add different subtypes under certain circumstances. For example, you can add a string and a number to obtain a number, but you can perform this summation only when the string is a number expressed as a string. For example, the output for the code
str = "2"
MsgBox num + str
is 3. But if you run the code
str = " for all"
MsgBox num + str
you get a subtype mismatch error because the + operator can't convert the string to a number. To obtain the string "1 for all", you need to use the string conversion (CStr) function to explicitly convert the number to a string:
str = " for all"
MsgBox CStr(num) + str
The CStr function modifies 1 into "1", allowing the + operator to work. (For more information about the CStr function, see my column in the June issue.) Although this kind of conversion is uncommon, it's a shortcut that's good to know.
Knowing how to add a date and a number to obtain a new later date is much more useful. For example, the code
returns tomorrow's date. The code first uses the Date function with empty parentheses to return the current system date and then applies the + operator to add 1. Similarly, you can use the - operator to obtain yesterday's date:
You can use the - operator with numbers and dates.
Two other noteworthy operators are those that divide numbers: the / and \ operators. The / operator handles standard division (i.e., divides two numbers with no additional operations). This operator doesn't introduce rounding errors and always returns a data value of subtype Double. For example, the code
B = 20
returns the result of 5. Although MsgBox doesn't display this value with a decimal point, VBScript considers it as subtype Double.
The other operator for division (\) works only with Integer values. The \ operator divides two numbers and carries out additional preliminary and final operations if necessary. If the numbers you're dividing aren't already Integer values, the \ operator first rounds them to an Integer subtype before dividing them. (The rounding doesn't change the values stored in variables A and B.) If the result of that division is a Double or Single value, the \ operator truncates the result to an Integer value by dropping the decimal point and any numbers thereafter.
Unfortunately, truncation can lead to errors. For example, the code
B = 3
returns a mathematically incorrect value of 6 because the \ operator rounds A to 20, divides 20 by 3, and truncates the result of 6.67 to 6. If you use the same input values with the / operator, you get the correct value.
As Table 2, page 8, shows, the comparison operators consist of the equality or assignment (=), inequality (<>), greater than (>), greater than or equal to (>=), less than (<), less than or equal to (<=), and object equivalence (Is) operators. These operators perform comparisons to determine whether the result is true, false, or null. You use these operators to compare data or objects.
Comparing data. Typically, you use the comparison operators to compare data of the same subtype. However, you might need to compare data with heterogeneous subtypes (e.g., comparing elements in heterogeneous arrays). If you compare a number and a string, the comparison operators always consider a string greater than a number. For example, the string "a" is greater than any number. If you compare a date with a string, you treat the date as a number. Thus, the string results are always greater than the date results.
In addition to the using the <, <=, >, >=, and = operators to compare strings, you might want to use the string comparison (StrComp) function. This function is more user-friendly than the comparison operators because it lets you compare strings regardless of their case. For example, suppose you use the = operator to compare the strings "Hello" and "hello" with the code
B = "hello"
MsgBox (A = B)
The result of this comparison is false because the = operator performs a binary comparison using the ASCII codes of the involved characters. The letters H and h have different ASCII codes (i.e., 72 and 104, respectively), so the result is false. As this example illustrates, binary comparisons are case-sensitive.
If your goal is to check the textual equality of the strings, using the StrComp function is much easier. You can use StrComp to perform either a binary or textual comparison of two strings. In this case, you want to compare text, so you use the VBScript constant vbTextCompare with the function. You specify this constant as the optional third argument; the first two arguments are the strings you want to compare. (You don't need to declare the vbTextCompare constant because it is an intrinsic constant.) For example, the code
B = "hello"
MsgBox StrComp(A, B, vbTextCompare)
performs a case-insensitive comparison to determine whether "Hello" and "hello" are the same string. StrComp returns a result of 0 if the strings are textually equal.
If you don't specify a third argument or you specify a value of 0 (the value for VBScript's vbBinaryCompare constant) as the third argument, StrComp performs a case-sensitive comparison. In any case, a result of 0 means the strings are equal, a result of 1 means the first string is greater than the second string, and a result of 1 means the first string is less than the second string.
Comparing objects. VBScript is an object-based language, so comparing objects with the Is operator is easy. (You can't use the = operator with objects.) The Is operator returns a result of true if two variables that contain objects are referring to the same instance of the same object. For example, consider the objects
Set B = CreateObject("Scripting
Set C = CreateObject("Shell
Variables A and B are different instances of the same object, and C is a different object. Thus, all the comparisons between these objects return a result of false. The Is operator considers variables A and B different objects because they're different instances of the same object. For the Is operator to return a result of true, you must have two variables pointing to the same instance of the same object. For example, the code
Set CopyObj = MyObj
clones the variable MyObj, producing a second copy of the same object instance. Thus, if you specify
you get the result of true.
Unlike Visual Basic for Applications (VBA), VBScript doesn't offer the Like operator. The Like operator lets you test whether a given string matches a given pattern. The Like operator is the only operator that VBScript lacks with respect to its parent language, VBA. However, in the sidebar "An Alternative for the Like Operator," page 10, I provide the IsLike function, a VBScript function you can use as a replacement for the Like operator in your scripts.
As Table 3 shows, the logical operators consist of the logical negation (Not), logical conjunction (And), logical disjunction (Or), logical exclusion (Xor), logical equivalence (Eqv), and logical implication (Imp) operators. You use these logical operators to not only build logical expressions but also manipulate numbers at the bit level. The operators return a result of either true or false.
And, Or, and Not are basic operators. If you need to determine whether two conditions exist, you use the And operator.
Suppose you need to determine whether A is equal to 1 (the first condition) and whether B is greater than 0 (the second condition). You use the syntax
result = expression1 And expression2
substituting (A=1) for expression1 and (B>0) for expression2:
You get a result of true only when both of the expressions you state are true.
If you need to determine whether at least one condition exists, you use the Or operator:
You get a result of true when at least one of the expressions is true.
You use the Not operator to negate a condition with code such as
If the original expression—in this case, (A=1)—is true, the result is false. If the original expression is false, the result is true.
Eqv, Xor, and Imp are advanced operators. If you need to make sure that only one of two conditions exists, you use the Xor operator. The Xor operator returns the result of true only when one expression is true and the other is false.
If you need to determine whether two conditions have the same value, you use the Eqv operator. The Eqv operator returns a result of true when both expressions have the same value.
You'll probably never need to use the Imp operator. The Imp operator always returns a result of true except when the first expression is true and the second expression is false.
The fourth family of operators is popular but small. It contains only the concatenation (&) operator. You use this operator to concatenate strings. For example, the code
Str2 = "world"
MsgBox Str1 & Str2
results in the string "Hello world". When joining two strings, the & operator produces the same result as the + operator. However, when you're joining heterogeneous data, the & and + operators aren't interchangeable. When the & operator encounters data that isn't of the subtype String, it automatically converts the data's subtype to its String representation before concatenation. The + operator doesn't perform this conversion, so you can't always use the operator to concatenate heterogeneous data. Thus, I recommend that you get in the habit of using the & operator instead of the + operator for concatenation.
A Word About Operator Precedence
Operator precedence is the order in which the system processes any expression that includes operators. In their respective families, the And, Not, *, \, and / precede all the other family members. In all other cases, the system processes operators in the order they appear, with one significant exception: The system processes the & operator after the arithmetic operators but before all other operators.
You can change this order by using parentheses. Any expression you enclose in parentheses takes precedence. Take, for example, the following code:
B = 2
The first MsgBox returns a result of 12 * 2, or 24. The second MsgBox returns a result of 10 + 4, or 14. If more than one expression is in parentheses, the system processes the innermost parentheses first.
Now that you're familiar with operator precedence, you can start using VBScript's arithmetic, comparison, logical, and concatenation operators. After you become comfortable using operators, you can use them with statements to combine variables and constants to produce new data.