VB Operators
An operator is a code element that performs an operation on one or more code elements that hold values. The value elements include variables, constants, literals, properties, returns from Function and Operator procedures, and expressions. MSDN - Operators and Expressions in Visual Basic
In Visual Basic the assignment operator is the single equal sign.
Arithmetic operators
Operator |
Use |
Example |
^ |
Exponentiation |
2 ^ 3 is eight, exponent may be a floating point number |
- |
Negation (used to reverse the sign of the given value, exp -intValue) |
-(-2) is two |
* |
Multiplication |
3 * 4 is twelve |
/ |
Floating point Division |
8 / 3 is 2.66666... |
\ |
Integer Division |
8 \ 3 is 2 |
Mod |
Modulus Arithmetic |
11 mod 3 is 2 |
+ |
Addition |
2 + 3 is five |
- |
Subtraction |
7 - 3 is four |
String concatenation operators
The ampersand character (&) is most often used to join string text. If numeric values are assigned to a string, then the result of the & and + will differ as shown in the example.
Operator |
Use |
Example |
+ |
String Concatenation |
strVal = "I" + " " + "love" + " " + "bananas."
strVal = 10 + (8 * 2.5) ' strVal is "30" |
& |
String Concatenation |
strVal = "I" & " love " & "bananas." (same as above)
strVal = 10 & (8 * 2.5) ' strVal is "1020" |
Comparison operators
Operator |
Use |
= |
Equality |
<> |
Inequality |
< |
Less than |
> |
Greater than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
Logical operators
Operator |
Use |
Not |
Negation |
And |
Conjunction |
AndAlso |
Conjunction |
Or |
Disjunction |
OrElse |
Disjunction |
Xor |
Disjunction |
The two new logical operators in VB.NET are AndAlso and OrElse and they are similar to the And and Or but do more.
The AndAlso operator will determine first if the left operand is true or false. If it is false, the expression does not bother evaluating the operand on the right. This is a kind of short-circuit that can save your program some processing time and possibly prevent errors. In this example the AndAlso determines that intVar1 < 10 is false, and so not bother prompting for a value for intVar2. If instead of the AndAlso operator, the And operator was used, then the user will be prompted for a value each time.
Similarly the OrElse operator determines if the first operand is true, and if it is, not bother evaluating the second operand.
Binary operators
Binary operators << and >> perform arithmetic shifting of integer-type numbers. Arithmetic shifting means that the bit that is shifted off either end of the number does not reappear at the other end. A form of bit-shifting called circular shifting is not directly supported by VB.NET.