Visual Basic Operators
Operators are symbols that perform operations on one or more values (operands). Visual Basic supports a wide range of operators to perform arithmetic, comparison, logical, bitwise, assignment, and other operations.
Arithmetic Operators
Used for mathematical calculations.
Operator | Description | Example |
---|---|---|
+ |
Addition | Dim sum As Integer = 5 + 3 ' sum will be 8 |
- |
Subtraction | Dim diff As Integer = 10 - 4 ' diff will be 6 |
* |
Multiplication | Dim product As Integer = 6 * 7 ' product will be 42 |
/ |
Division (returns a floating-point number) | Dim quotient As Double = 10.0 / 4.0 ' quotient will be 2.5 |
\ |
Integer Division (returns the integer part of the division) | Dim intQuotient As Integer = 10 \ 4 ' intQuotient will be 2 |
Mod |
Modulus (returns the remainder of a division) | Dim remainder As Integer = 10 Mod 4 ' remainder will be 2 |
^ |
Exponentiation | Dim power As Double = 2.0 ^ 3.0 ' power will be 8.0 |
Comparison Operators
Used to compare two values. They return a Boolean value (True
or False
).
Operator | Description | Example |
---|---|---|
= |
Equal to | If x = y Then ... |
<> |
Not equal to | If x <> y Then ... |
< |
Less than | If x < y Then ... |
> |
Greater than | If x > y Then ... |
<= |
Less than or equal to | If x <= y Then ... |
>= |
Greater than or equal to | If x >= y Then ... |
Is |
Compares two reference types for equality (are they the same object?) | If obj1 Is obj2 Then ... |
IsNot |
Compares two reference types for inequality | If obj1 IsNot obj2 Then ... |
Like |
Compares a string against a pattern | If text Like "A*" Then ... |
Logical and Bitwise Operators
Used to combine Boolean expressions or to perform bitwise operations on integers.
Operator | Description | Example |
---|---|---|
And |
Logical AND (evaluates both operands) | If x > 0 And y > 0 Then ... |
Or |
Logical OR (evaluates both operands) | If x > 0 Or y > 0 Then ... |
Xor |
Logical XOR (evaluates both operands) | If x > 0 Xor y > 0 Then ... |
Not |
Logical NOT | If Not x > 0 Then ... |
AndAlso |
Short-circuiting Logical AND (evaluates right operand only if left is True) | If x > 0 AndAlso y > 0 Then ... |
OrElse |
Short-circuiting Logical OR (evaluates right operand only if left is False) | If x > 0 OrElse y > 0 Then ... |
& |
Bitwise AND | Dim result As Integer = 5 & 3 ' result will be 1 (binary 0101 & 0011 = 0001) |
| |
Bitwise OR | Dim result As Integer = 5 | 3 ' result will be 7 (binary 0101 | 0011 = 0111) |
^ |
Bitwise XOR | Dim result As Integer = 5 ^ 3 ' result will be 6 (binary 0101 ^ 0011 = 0110) |
~ |
Bitwise NOT (complement) | Dim result As Integer = ~5 ' result will be -6 (for a 32-bit integer) |
<< |
Bitwise Left Shift | Dim result As Integer = 5 << 1 ' result will be 10 |
>> |
Bitwise Right Shift | Dim result As Integer = 10 >> 1 ' result will be 5 |
Assignment Operators
Used to assign a value to a variable.
Operator | Description | Example |
---|---|---|
= |
Assignment | x = 10 |
+= |
Add and assign | x += 5 ' Equivalent to x = x + 5 |
-= |
Subtract and assign | x -= 3 ' Equivalent to x = x - 3 |
*= |
Multiply and assign | x *= 2 ' Equivalent to x = x * 2 |
/= |
Divide and assign | x /= 4 ' Equivalent to x = x / 4 |
\= |
Integer divide and assign | x \= 3 ' Equivalent to x = x \ 3 |
&= |
Concatenate and assign (for strings) | str1 &= str2 ' Equivalent to str1 = str1 & str2 |
&&= |
Bitwise AND and assign | x &&= y ' Equivalent to x = x & y |
|= |
Bitwise OR and assign | x |= y ' Equivalent to x = x | y |
^= |
Bitwise XOR and assign | x ^= y ' Equivalent to x = x ^ y |
<<= |
Bitwise left shift and assign | x <<= 2 ' Equivalent to x = x << 2 |
>>= |
Bitwise right shift and assign | x >>= 1 ' Equivalent to x = x >> 1 |
Other Operators
Includes operators for string concatenation, object comparison, type checking, and more.
- Concatenation Operator:
&
- Joins two strings. - String Interpolation:
$
- Allows embedding expressions within string literals.Dim name As String = "World"; Dim message As String = $"Hello, {name}!"
- Range Operator:
To
- Used inFor...Next
loops. - Member Access Operator:
.
- Used to access members of a class or structure. - Address-Of Operator:
AddressOf
- Returns a delegate pointing to a procedure. - GetType Operator:
GetType
- Returns theType
object of a specified type or variable. - TypeOf...Is Operator:
TypeOf...Is
- Determines if an object is compatible with a given data type. - Function Member Operator:
Function()
- Used for anonymous methods and lambda expressions.
Operator Precedence
Operators are evaluated in a specific order. Operators with higher precedence are evaluated before operators with lower precedence. You can use parentheses ()
to override the default precedence.
For a detailed table of operator precedence, please refer to the official VB.NET operator precedence chart.