VB.NET Operators
Operators are special symbols or keywords that perform operations on one or more values (operands). VB.NET provides a rich set of operators for various tasks, including arithmetic, comparison, logical operations, and more. Understanding operators is fundamental to writing efficient and powerful VB.NET code.
Arithmetic Operators
These operators are used to perform mathematical calculations.
Operator | Description | Example |
---|---|---|
+ |
Addition | Dim sum As Integer = 5 + 3 ' sum will be 8 |
- |
Subtraction | Dim difference As Integer = 10 - 4 ' difference will be 6 |
* |
Multiplication | Dim product As Integer = 6 * 7 ' product will be 42 |
/ |
Division (returns a floating-point result) | Dim quotient As Double = 10 / 4 ' quotient will be 2.5 |
\ |
Integer Division (returns the integer part of the result) | Dim intQuotient As Integer = 10 \ 4 ' intQuotient will be 2 |
Mod |
Modulo (returns the remainder of a division) | Dim remainder As Integer = 10 Mod 3 ' remainder will be 1 |
^ |
Exponentiation | Dim power As Double = 2 ^ 3 ' power will be 8.0 |
Comparison Operators
These operators compare two values and return a Boolean value (True
or False
).
Operator | Description | Example |
---|---|---|
= |
Equal to | If age = 18 Then ... |
<> |
Not equal to | If name <> "Admin" Then ... |
< |
Less than | If score < 60 Then ... |
> |
Greater than | If price > 100 Then ... |
<= |
Less than or equal to | If quantity <= 5 Then ... |
>= |
Greater than or equal to | If rating >= 4 Then ... |
Is |
Checks if two object variables refer to the same object | If obj1 Is obj2 Then ... |
IsNot |
Checks if two object variables refer to different objects | If obj1 IsNot obj2 Then ... |
Logical Operators
These operators combine Boolean expressions.
Operator | Description | Example |
---|---|---|
And |
Logical AND (True if both operands are True) | If isLoggedOn And isAdmin Then ... |
Or |
Logical OR (True if at least one operand is True) | If hasPermission Or isGuest Then ... |
Xor |
Logical Exclusive OR (True if exactly one operand is True) | If option1 Xor option2 Then ... |
Not |
Logical NOT (Inverts the Boolean value) | If Not isComplete Then ... |
AndAlso |
Short-circuiting Logical AND (evaluates right operand only if left is True) | If user IsNot Nothing AndAlso user.IsActive Then ... |
OrElse |
Short-circuiting Logical OR (evaluates right operand only if left is False) | If isExpired OrElse hasTrial Then ... |
String Concatenation Operator
This operator joins strings together.
Operator | Description | Example |
---|---|---|
& |
Concatenates strings | Dim fullName As String = firstName & " " & lastName |
Bitwise Operators
These operators perform operations on the individual bits of their operands.
Operator | Description | Example |
---|---|---|
And |
Bitwise AND | Dim result As Integer = 5 And 3 ' result is 1 (binary 0101 And 0011 = 0001) |
Or |
Bitwise OR | Dim result As Integer = 5 Or 3 ' result is 7 (binary 0101 Or 0011 = 0111) |
Xor |
Bitwise Exclusive OR | Dim result As Integer = 5 Xor 3 ' result is 6 (binary 0101 Xor 0011 = 0110) |
Not |
Bitwise NOT | Dim result As Integer = Not 5 ' result is -6 (assuming 32-bit signed integer) |
<< |
Left Shift | Dim result As Integer = 5 << 1 ' result is 10 (binary 0101 shifted left by 1 becomes 1010) |
>> |
Right Shift | Dim result As Integer = 10 >> 1 ' result is 5 (binary 1010 shifted right by 1 becomes 0101) |
Assignment Operators
These operators assign values to variables.
Operator | Description | Example |
---|---|---|
= |
Assigns the value of the right operand to the left operand | x = 5 |
+= |
Adds the right operand to the left operand and assigns the result to the left operand | x += 3 ' equivalent to x = x + 3 |
-= |
Subtracts the right operand from the left operand and assigns the result to the left operand | x -= 2 ' equivalent to x = x - 2 |
*= |
Multiplies the left operand by the right operand and assigns the result to the left operand | x *= 4 ' equivalent to x = x * 4 |
/= |
Divides the left operand by the right operand and assigns the result to the left operand | x /= 2 ' equivalent to x = x / 2 |
\= |
Performs integer division of the left operand by the right operand and assigns the result to the left operand | x \= 3 ' equivalent to x = x \ 3 |
&= |
Concatenates the right operand to the left operand and assigns the result to the left operand | str &= " World" ' equivalent to str = str & " World" |
&&= |
Bitwise AND and assign | x &&= 1 ' equivalent to x = x && 1 |
||= |
Bitwise OR and assign | x ||= 1 ' equivalent to x = x Or 1 |
^= |
Bitwise XOR and assign | x ^= 1 ' equivalent to x = x Xor 1 |
<<= |
Left shift and assign | x <<= 2 ' equivalent to x = x << 2 |
>>= |
Right shift and assign | x >>= 1 ' equivalent to x = x >> 1 |
Other Operators
VB.NET also includes other useful operators.
Operator | Description | Example |
---|---|---|
. |
Member Access Operator (accesses members of a class, structure, or module) | Dim myCar As New Car() myCar.Model = "Sedan" |
( ) |
Parentheses (used for grouping expressions or calling methods) | result = (a + b) * c Console.WriteLine("Hello") |
& (unary) |
Address-of Operator (returns the memory address of a variable) | Dim ptr As IntPtr = AddressOf myVariable |
Of |
For generic type arguments | Dim myList As New List(Of Integer) |
From |
Used in LINQ queries | Dim numbers = From n In numberList Where n > 10 Select n |
Like |
Pattern matching for strings | If fileName Like "*.txt" Then ... |
CType |
Explicit type conversion | Dim numStr As String = "123" Dim numInt As Integer = CType(numStr, Integer) |
If...Then...Else (ternary operator) |
Conditional assignment | Dim status As String = If(isComplete, "Completed", "Pending") |
Understanding the precedence and associativity of operators is crucial for writing correct expressions. VB.NET follows standard operator precedence rules.