Operators in Visual Basic

Operators are special symbols that perform operations on one or more values (operands). Visual Basic .NET supports a rich set of operators for various purposes, including arithmetic, comparison, logical operations, and assignments.

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 result As Double = 10 / 4 ' result will be 2.5
\ Integer Division (discards the fractional part) Dim quotient As Integer = 10 \ 4 ' quotient will be 2
Mod Modulo (returns the remainder of a division) Dim remainder As Integer = 10 Mod 4 ' remainder will be 2
^ Exponentiation Dim power As Integer = 2 ^ 3 ' power will be 8

Comparison Operators

These operators are used to compare two values. They return a Boolean value (True or False).

Operator Description Example
= Equal to If x = 5 Then ...
<> Not equal to If name <> "Admin" Then ...
< Less than If age < 18 Then ...
> Greater than If score > 90 Then ...
<= Less than or equal to If count <= 10 Then ...
>= Greater than or equal to If total >= 100 Then ...
Is Checks if two object references point to the same object If obj1 Is obj2 Then ...
IsNot Checks if two object references point to different objects If obj1 IsNot obj2 Then ...
Like Performs pattern matching on strings If name Like "A*" Then ...

Logical Operators

These operators combine Boolean expressions.

Operator Description Example
And Logical AND (returns True if both operands are True) If isLoggedOn And hasPermission Then ...
Or Logical OR (returns True if at least one operand is True) If isSunday Or isHoliday Then ...
Xor Logical XOR (returns True if exactly one operand is True) If isMale Xor isFemale Then ...
Not Logical NOT (reverses the Boolean value of the operand) If Not isComplete Then ...
AndAlso Short-circuiting AND (evaluates the second operand only if the first is True) If number > 0 AndAlso (10 / number) > 1 Then ...
OrElse Short-circuiting OR (evaluates the second operand only if the first is False) If errorOccurred OrElse (retryCount < 3) Then ...

Assignment Operators

These operators assign a value to a variable.

Operator Description Example
= Assigns the value of the right operand to the left operand x = 10
+= Adds the right operand to the left operand and assigns the result x += 5 ' Equivalent to x = x + 5
-= Subtracts the right operand from the left operand and assigns the result x -= 3 ' Equivalent to x = x - 3
*= Multiplies the left operand by the right operand and assigns the result x *= 2 ' Equivalent to x = x * 2
/= Divides the left operand by the right operand and assigns the result x /= 4 ' Equivalent to x = x / 4
\= Performs integer division and assigns the result x \= 2 ' Equivalent to x = x \ 2
&= Concatenates the right operand to the left operand and assigns the result myString &= " World" ' Equivalent to myString = myString & " World"

String Concatenation Operator

The & operator is used to combine strings.

Operator Description Example
& Concatenates two strings Dim greeting As String = "Hello" & " " & "World" ' greeting will be "Hello World"

Other Operators

Visual Basic .NET also includes other useful operators.

Note: Operators have an order of precedence, which determines the order in which operations are performed in an expression. Parentheses can be used to override this order.
Tip: Using `AndAlso` and `OrElse` can improve performance by preventing unnecessary evaluation of expressions, especially when dealing with potentially null values or expensive operations.