
86
|
Chapter 5: Operators
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
understandings of addition and subtraction were reversed. However, such prac-
tices will make the code more difficult to understand and debug.
For further details about operator overloading, see the “Operator Statement”
entry in Chapter 12.
Operator Precedence
If you include more than one operator in a single line of code, you need to know
the order in which VB will evaluate them. Otherwise, the results may be
completely different from what you intended. For instance, the following
statement:
x = 5 + 3 * 7
could be interpreted as:
x = (5 + 3) * 7 ' --> 56
or as:
x = 5 + (3 * 7) ' --> 26
The rule that defines the order in which a language processes operators is known
as the order of precedence. If the order of precedence results in operations being
evaluated in an order other than the intended one, you can explicitly override the
order of precedence through the use of parentheses. Indeed, complex (or even
relatively simple) expressions should include parentheses to avoid any compiler
misinterpretation or human confusion. (By the way, the example, once paren-
theses are removed, evaluates to 26.)
When multiple operators appear at the same level of evaluation (that is, they are
not subgrouped with parentheses), they are processed in a specific order of prece-
dence. In