The Visual Basic operators fall into five main categories: arithmetic, concatenation, comparison, logical, and bitwise. The following sections explain these categories and the operators they contain. The end of this appendix describes special Date and TimeSpan operators, as well as operator overloading.
The following table lists the arithmetic operators provided by Visual Basic.
OPERATOR | PURPOSE | EXAMPLE | RESULT |
---|---|---|---|
| Exponentiation |
| (2 to the power 3) = |
| Negation |
|
|
| Multiplication |
|
|
| Division |
|
|
Integer division |
|
| |
| Modulus |
|
|
| Addition |
|
|
| Subtraction |
|
|
| Bit left shift |
|
|
| Bit right shift |
|
|
The bit shift operators deserve a little extra discussion. These operators shift the binary representation of a number by a given number of bits either left or right. Unfortunately, Visual Basic doesn't understand binary so you must manually translate between binary and decimal, octal, or hexadecimal.
For example, the hexadecimal value &H57 is 01010111 in binary. If you shift this one bit to the left, you get 10101110, which is &HAE in hexadecimal. If you shift the original value one bit to the right, you get 00101011, which is &H2B in hexadecimal.
When working with binary values, many developers prefer to work in hexadecimal because each hexadecimal digit corresponds to four binary bits so you can work with each group of four bits separately.
Visual Basic provides two concatenation operators: + and &. Both join two strings together. Because the + symbol also represents an arithmetic operator, your code will be easier to read if you use the & symbol for concatenation.
The following table lists the comparison operators provided by Visual Basic.
OPERATOR | PURPOSE | EXAMPLE | RESULT |
---|---|---|---|
| Equals |
| True if A equals B |
| Not equals |
| True if A does not equal B |
| Less than |
| True if A is less than B |
| Less than or equal to |
| True if A is less than or equal to B |
| Greater than |
| True if A is greater than B |
| Greater than or equal to |
| True if A is greater than or equal to B |
| Equality of two objects |
| True if emp and mgr refer to the same object |
| Inequality of two objects |
| True if emp and mgr refer to different objects |
| Object is of a certain type |
| True if obj points to a Manager object |
| Matches a text pattern |
| True if value contains three digits, a dash, and four digits |
The following table lists characters that have special meanings to the Like operator.
CHARACTER(S) | MEANING |
---|---|
| Matches any single character |
| Matches any zero or more characters |
| Matches any single digit |
| Matches any of the characters between the brackets |
| Matches any character not between the brackets |
| When inside brackets, matches any character in the range A to Z |
The following table lists some useful Like patterns.
PATTERN | MEANING |
---|---|
| Seven-digit U.S. phone number |
| Ten-digit U.S. phone number including area code |
| Eleven-digit U.S. phone number beginning with 1 and area code |
| Five-digit U.S. ZIP code |
| Nine-digit U.S. ZIP+4 code |
| e-mail address |
| Canadian postal code |
The following table summarizes the Visual Basic logical operators.
OPERATOR | PURPOSE | EXAMPLE | RESULT |
---|---|---|---|
| Logical or bitwise negation |
| True if A is false |
| Logical or bitwise And |
| True if A and B are both true |
| Logical or bitwise Or |
| True if A or B or both are true |
Logical or bitwise exclusive Or |
| True if A or B but not both is true | |
| Logical or bitwise And with short-circuit evaluation |
| True if A and B are both true |
| Logical or bitwise Or with short-circuit evaluation |
| True if A or B or both are true |
Bitwise operators work much as logical operators do, except that they compare values one bit at a time. Visual Basic provides bitwise versions of Not, And, Or, and Xor but not bitwise versions of AndAlso or OrElse.
The following table lists the operators in order of precedence. When evaluating an expression, the program evaluates an operator before it evaluates those lower than it in the list. When operators are on the same line, the program evaluates them from left to right.
OPERATOR | DESCRIPTION |
---|---|
| Exponentiation |
| Negation |
| Multiplication and division |
Integer division | |
| Modulus |
| Addition, subtraction, and concatenation |
| Concatenation |
| Bit shift |
All comparisons | |
| Logical and bitwise negation |
| Logical and bitwise And with and without short-circuit evaluation |
| Logical and bitwise Xor, and Or with and without short-circuit evaluation |
Use parentheses to change the order of evaluation and to make expressions easier to read.
The following table summarizes the Visual Basic assignment operators.
OPERATOR | EXAMPLE | ORIGINAL SYNTAX EQUIVALENT |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
There are no assignment operators corresponding to Mod or the Boolean operators.
The Choose, If, and IIf statements return values that you can assign to a variable. These statements are not really assignment operators (you need to use = to assign their results to a variable) and they perform decisions so they are described in Appendix E, "Control Statements."
The Date and TimeSpan data types are related through their operators. The following list shows the relationships between these two data types:
Date -
Date = TimeSpan
Date +
TimeSpan = Date
TimeSpan +
TimeSpan = TimeSpan
TimeSpan -
TimeSpan = TimeSpan
The following table lists convenient methods provided by the Date data type.
SYNTAX | MEANING |
---|---|
| Returns date1 plus timespan1 |
| Returns the date plus the indicated number of years |
| Returns the date plus the indicated number of months |
| Returns the date plus the indicated number of days |
| Returns the date plus the indicated number of hours |
| Returns the date plus the indicated number of minutes |
| Returns the date plus the indicated number of seconds |
| Returns the date plus the indicated number of milliseconds |
| Returns the date plus the indicated number of ticks (100 nanosecond units) |
| Returns the time span between date2 and date1 |
| Returns a value indicating whether date1 is greater than, less than, or equal to date2 |
| Returns True if date1 equals date2 |
The syntax for defining an operator for a class is as follows:
[<attributes>
] Public [ Overloads ] Shared [ Shadows ] _ [ Widening | Narrowing ] Operatorsymbol
(operands
)As type
... End Operator
The operator's symbol can be +, -, *, /, , ^, &, <<, >>, =, <>, <, <, <=, >=, Mod, Not, And, Or, Xor, Like, IsTrue, IsFalse
, or CType
.
For example, the following code defines the + operator for the ComplexNumber class. This class has two public properties, Re and Im, that give the number's real and imaginary parts.
Public Shared Operator +( ByVal c1 As ComplexNumber, ByVal c2 As ComplexNumber) As ComplexNumber Return New ComplexNumber With { .Re = c1.Re + c2.Re, .Im = c1.Im + c2.Im} End Operator
Some operands come in pairs, and if you define one, you must define the other. The pairs are =
and <>, <
and >, <=
and >=
, and IsTrue
and IsFalse
.
If you define And and IsFalse
, Visual Basic uses them to define the AndAlso
operator. Similarly, if you define Or
and IsTrue
, Visual Basic automatically provides the OrElse
operator.