Operators come in two usage types: unary and binary. Unary operators work on a single operand, while binary operators require two operands. Most operators in Visual Basic are binary operators.
Arithmetic Operators
The VB arithmetic operators provide basic manipulation of integer and floating point numbers. They could be called "the calculator operators," since most of them appear on even the most basic four-function calculator.
+(Addition)
The addition operator adds numeric expressions together and returns the result.
result = expression1 + expression2
When used with string operands, the + operator acts like the & string concate nation operator, as described below.
+ (Unary Plus)
Usually, the + operator only appears as a binary operator. But it can be used in a unary form. In this usage, when placed immediately before a number or numeric expression, it ensures that the expression retains its sign, either positive or negative. Since expressions retain their sign by default, the unary plus operator is redundant and rarely used.
result =+expression
New variation in 2005. Beginning with the 2005 release of Visual Basic, overloading this operator may prove useful in some classes.
- (Subtraction)
The subtraction operator deducts the value of one expression from another, returning the difference.
result = expression1 -expression2
Unlike the addition operator, the subtraction operator cannot be used with string operands.
- (Unary Negation)
The - operator performs double duty as both a unary and binary operator. In its unary form, when placed immediately before a number or numeric expression, it negates the expression, effectively multiplying the expression by -1.
result =-expression
* (Multiplication)
The multiplication operator multiplies two numeric expressions together and returns the result.
result = expression1 * expression2
/ (Division)
The division operator divides one numeric expression into another and returns the result, retaining any decimal remainder. If the second operand is zero (0), a "divide by zero" error occurs.
result = expression1 / expression2
\ (Integer Division)
The integer division operator works just like the normal division operator, but any decimal remainder is truncated (not rounded) before returning the result. If the second operand is zero (0), a "divide by zero error" occurs.
result = expression1 \ expression2
This operator always returns a non-decimal data type (such as Short, Integer, or Long), even if the original operands were decimal.
Mod (Modulo)
The modulo operator divides one numeric expression into another and returns only the remainder as a whole number, also known as the modulus. If either of the two source expressions are decimal numbers, they are rounded to integer values prior to the modulo operation. To obtain expected results, explicitly truncate or round decimal expressions before using them as operands. The return value is a nonnegative integral data type.
As an example, the expression:
10 Mod 3
returns 1, because the remainder of 10 divided by 3 is 1.
result = expression1 Mod expression2
^ (Exponentiation)
The exponentiation operator raises one numeric expression to the power of the second and returns the result.
Concatenation operators connect two source string expressions together and return a single string joined from the two original strings. Because strings in .NET are immutable, the returned string is always a completely new string instance.
& (String Concatenation)
The string concatenation operator returns a concatenated string from two source string expressions. Any non-string source expression is first converted to a string prior to concatenation (even if OptionStrict is set to On).
result = expression1 & expression2
+ (Addition)
When the addition operator is used with string operands, it concatenates the operands instead of adding their values. However, using this operator for concatenation can make the source code unclear, especially when using the new .NET-recommended variable naming conventions. If you mix string and numeric operands, this operator may also cause compile-time or runtime errors, depending on the content of the operands. For the clearest code, use the & concatenation operator instead.
Logical and Bitwise Operators
Logical operators evaluate one or more expressions and return a Boolean result (True or False). VB supports six logical operators, many of which can also be used as bitwise operators, along with two bitwise-only operators. Bitwise operations work on integral (numeric integer) operands at the bit level and return numeric results. Other languages, such as C#, include distinct logical and bitwise operators, but for historical reasons, VB mostly uses a common set of operators for both types of operations.
If any of the operands are numeric (that is, non-Boolean), a bitwise operation is done instead of a logical operation. In cases where one operand is Boolean and the other is not, the Boolean operand is converted to a number first, using 0 for False and -1 for True.
In performing some logical operations, the .NET versions of Visual Basic use conditional short-circuiting, where complex conditional expressions are only partially evaluated if the final result of the entire expression can be determined without full evaluation. Individual expressions within a larger compound expression are evaluated only until the expressions overall value is known, unless one of the individual expressions involves a call to another function or subroutine. Short-circuiting can occur in logical AndAlso operations when the first operand evaluates to False, as well as in logical OrElse operations when the first operand evaluates to True. When using the more common And and Or operators, no short-circuiting is done.
Boolean operations always use the two Boolean values of True and False. Although Visual Basic's Boolean data type is based on the underlying .NET System.Boolean data type, its use in Visual Basic differs from that of other .NET languages. For historical reasons, Visual Basic's True value, when converted to a number, equates to -1. Other .NET languages--specifically C#--use a value of 1 for True. Although .NET resolves this difference through the shared data type, it can become an issue if you use a non-.NET data transfer method (such as a plain text file) to share numeric Boolean data between .NET languages.
The And operator performs a logical or bitwise conjunction on the two source operands. In logical operations, it returns True if and only if both operands evaluate to True. If either operand is False, then the result is False. The syntax is:
result = expression1 And expression2
For example, consider the following statement:
If (x = 5) And (y < 7) Then
In this case, the code within the Then clause will be executed only if the value of x is 5 and the value of y is less than 7.
As a bitwise operator, And returns 1 in a bit position if the compared bits in the same position in both expressions are 1, and it returns 0 in all other cases, as shown in the following table:
Bit in expression1 0
Bit in expression2 0
Result 0
0
1
0
1
0
0
1
1
1
For example, the bitwise result of 15 And 179 is 3, as the following binary representation shows:
00001111 And 10110011 -> 00000011
AndAlso
The AndAlso operator works exactly like the logical And operator, but short-circuiting is enabled. If the first operand evaluates to False, the second operand is not evaluated at all, even if that expression includes function calls. Operands are evaluated from left to right. AndAlso does not perform bitwise operations.
Or
The Or operator performs a logical or bitwise disjunction on the two source operands. In logical operations, it returns True if either of the operands evaluates to True. If both operands are False, then the result is False. The syntax is:
result = expression1 Or expression2
For example, consider the following statement:
If (x = 5) Or (y < 7) Then
In this case, the code within the Then clause will be executed if either the value of x is 5 or the value of y is less than 7.
As a bitwise operator, Or returns 1 in a bit position if either of the compared bits in the same position in the source expressions are 1, and it returns 0 in all other cases, as shown in the following table:
Bit in expression1 0
Bit in expression2 0
Result 0
0
1
1
1
0
1
1
1
1
For example, the bitwise result of 15Or179 is 191, as the following binary representation shows:
00001111 Or 10110011 -> 10111111
OrElse
The OrElse operator works exactly like the logical Or operator, but short-circuiting is enabled. If the first operand evaluates to True, the second operand is not evaluated at all, even if that expression includes function calls. Operands are evaluated from left to right. OrElse does not perform bitwise operations.
Not
The Not operator performs a logical or bitwise negation on a single expression. In logical operations, it returns True if the operand is False, and False if the operand is True. The syntax is:
result = Not expression1
For example, consider the following statement:
If Not IsNumeric(x) Then
In this example, the code within the Then clause will be executed if IsNumeric returns False, indicating that x is not a value capable of being represented by a number.
As a bitwise operator, Not simply toggles the value of each bit in the source expression between 0 and 1, as shown in the following table:
Bit in expression1
Result
0
1
1
0
For example, the bitwise result of Not 16 is 239, as the following binary representation shows:
Not 00010000 -> 11101111
Xor
The Xor (an abbreviation for "eXclusive OR") operator performs a logical or bitwise exclusion on the two source operands. In logical operations, it returns True if and only if the two expressions have different truth values. If both expressions are True, or both are False, this operator returns False. If one of the operands is True but the other False, then Xor returns True. The syntax is:
result = expression1 Xor expression2
As a bitwise operator, Xor returns 1 in a bit position if the compared bits are different from each other, and it returns 0 if they are the same, as shown in the following table:
Bit in expression1 0
Bit in expression2 0
Result 0
0
1
1
1
0
1
1
1
0
For example, the result of 15 Xor 179 is 188, as the following binary representation shows:
00001111 Xor 10110011 -> 10111100
Eqv and Imp
Eqv and Imp, two logical and bitwise operators present in VB 6, have been removed from .NET implementations of Visual Basic. Eqv can be replaced with the = (equal to) comparison operator. The expression:
expression1 Eqv expression2
is the same as the logical comparison:
expression1 = expression2
Imp can be replaced with a logical expression using the Not and Or operators. For example:
expression1 Imp expression2
can also be expressed as:
(Not expression1) Or expression2
If you need more precise replacements using bitwise calculations, see the "Logical and Bitwise Operators" section in Appendix D.
<< (Shift Left)
New in 2003. The << (shift left) operator performs a left shift of the bits in the first operand by the number of bits specified in the second operand. All bits shifted off the left are lost. All bits newly vacated on the right are filled with zeros.
The number of bits you can shift is limited by the number of possible bits in the first operand. Any excess number of shift positions will be ignored. This operator never throws an overflow exception. The syntax is:
result = source << bits
For example, the bitwise result of 15 << 5 is 224, as the following binary representation shows:
00001111 << 5 -> 11100000
>> (Shift Right)
New in 2003. The >> (shift right) operator performs a right shift of the bits in the first operand by the number of bits specified in the second operand. All bits shifted off the right are lost. All bits newly vacated on the left are filled with the bit value of the leftmost bit position before shifting. When shifting unsigned data values (Byte , UShort, UInteger, ULong), the newly vacated bits on the left are filled with zero (0).
The number of bits you can shift is limited by the number of possible bits in the first operand. Any excess number of shift positions will be ignored. This operator never throws an overflow exception. The syntax is:
result = source >> bits
For example, the bitwise result of 12 >> 1 is 6, as the following binary representation shows:
Along with the standard assignment operator (=), many other operators can be turned into assignment operators by simply appending an equals sign to the right of the operator. These converted operators all have the same form:
expression1 <operator>= expression2
where <operator> is the operator being promoted to an assignment operator. This form is equivalent to:
expression1 = expression1 <operator> expression2
To illustrate, consider the addition assignment operator. The expression:
x += 1
is equivalent to:
x = x + 1
which simply adds 1 to the value of x . Similarly, the expression:
s &= "end"
is equivalent to:
s = s & "end"
which concatenates the string "end" to the end of the string s.
All of these "shortcut" assignment operators were introduced with Visual Basic .NET 2002.
= (Assignment)
The assignment operator assigns the value or reference of the expression on the right of the assignment operator to the variable on the left. For example, the following assigns y plus an additional value of 5 to x .
x = y + 5
The assignment operator alone is used to assign both values and references; in previous versions of VB, the Set statement had to be used along with the assignment operator to assign an object reference. The Set keyword is no longer used in this context. Also, the previously optional Let keyword is no longer part of the Visual Basic language.
+=
The addition assignment operator. As an example:
totalValue += 1
adds 1 to the value of totalValue and assigns the result to totalValue.
-=
The subtraction assignment operator. As an example:
totalValue -= 1
subtracts 1 from the value of totalValue and assigns the result to totalValue.
*=
The multiplication assignment operator. As an example:
totalValue *= 3
multiplies the value of totalValue by 3 and assigns the result to totalValue.
/=
The division assignment operator. As an example:
totalValue /= 2
divides the value of totalValue by 2 and assigns the result to totalValue. If the value to the right of the division assignment operator equates to 0, an error occurs.
\=
The integer division assignment operator. As an example:
totalValue \= 2
divides the value of totalValue by 2, discards any fractional part, and assigns the result to totalValue. If the value to the right of the integer division assignment operator equates to 0, an error occurs.
^=
The exponentiation assignment operator. As an example:
totalValue ^= 2
squares the value of totalValue and assigns the result to totalValue.
&=
The concatenation assignment operator. As an example:
storyText &= "The End"
appends a literal text string to the end of storyTexts existing content and assigns this new concatenated string to storyText.
<<=
New in 2003. The shift left assignment operator. As an example:
dataMask <<= 2
shifts the bits of dataMask left two positions and assigns the new value back to dataMask.
>>=
New in 2003. The shift right assignment operator. As an example:
dataMask >>= 2
shifts the bits of dataMask right two positions and assigns the new value back to dataMask.
Unlike the comparison operators, in which the order of symbols is reversible (that is, >= is the same as =>), the order of the "shortcut" assignment operator symbols is not reversible. For example, while:
x -= 1
decrements x by 1, the expression :
x =- 1
assigns a value of 1 to the variable x . That is, it really looks like this:
x = -1
Please check back next week for the conclusion to this article.