Updated March 14, 2023
Introduction to VB.NET Operators
Visual Basic .Net by Microsoft is a type of object-oriented programming language that is employed on the .Net framework. It allows multiple operations and corresponding operators to enable the programmers to incorporate the same in the logical units of the programs. The operators used in VB .Net programming language are Arithmetic Operators (+, -, *, /, ^, etc), Comparison Operators (=, <>, >, <, >=, <=, etc), Logical Operators (And, Or, Not, IsFalse, IsTrue, etc), Bit Shift Operators (<<, >>, Xor, etc), Assignment Operators (=, +=, /=, ^=, etc) and Miscellaneous Operators (Await, GetType, If, etc).
What are operators in VB.NET?
Operators are special symbols that are used to perform specific types of operations. Operators perform a very special role as they make computation and operations easier. Let us see some of the types of VB.NET Operators :
- Arithmetic Operators.
- Comparison Operators.
- Logical/Bitwise Operators.
- Bit Shift Operators.
- Assignment Operators.
- Miscellaneous Operators
Types of VB.NET Operators
These are some of the types of VB.NET Operators:
For example:
x = 2 + 3
Here, = and + are the operators and x, 2, 3 are the operands. The operator is working on some things, those things are known as an operand.
VB.NET Operators are a rich set of operators that are available for use.
1. Arithmetic Operators
Arithmetic operators are used for performing mathematical operations like addition, subtraction, division, multiplication, etc.
Arithmetic operators in VB.NET
Operators | Meaning | Example |
^ | Raises one operand to the power of another | x ^ y (x to the power y) |
+ | Adds two operands | x + y |
– | Subtracts second operand from the first | x – y |
* | Multiplies both operands | x * y |
/ | Divides one operand by another and returns a floating-point result | x / y |
\ | Divides one operand by another and returns an integer result | x \ y |
MOD | Modulus Operator and the remainder of a result after an integer division | x MOD y (remainder of x/y) |
Example #1: Arithmetic operators in VB.NET
Module operators
Sub Main()
Dim x As Integer = 15
Dim y As Integer = 3
' Output: x + y = 18
Console.WriteLine("x + y: {0}", x+y)
' Output: x - y = 12
Console.WriteLine("x - y: {0}", x-y)
' Output: x * y = 45
Console.WriteLine("x * y: {0}", x*y)
' Output: x / y = 5
Console.WriteLine("x / y: {0}", x/y)
' Output: x \ y = 5
Console.WriteLine("x \ y: {0}", x\y)
' Output: x MOD y = 0
Console.WriteLine("x MOD y: {0}", x Mod y)
' Output: x ^ y = 3375
Console.WriteLine("x ^ y: {0}", x^y)
End Sub
End Module
Output:
2. Comparison Operators
Comparison operators are basically used to compare different values. These operators normally return Boolean values either True or False depending upon the condition.
Comparison operators in VB.NET
Operators | Meaning | Example |
= | Equality Check -Returns True if both values are the same | x == y |
<> | Non-Equality Returns True if both values are unequal | x < > y |
> | Greater than Check-Returns true if the first value specified is greater than the second | x > y |
< | Less than-Returns true if the first value specified is less than second | x < y x |
>= | Checks for two conditions, If the first value is greater than or equal to the second value it returns true | >= y |
<= | Checks for two conditions, If the first value is less than or equal to the second value it returns true | x <= y |
Is | Compares two Object Variable for Reference, True If the same object reference | |
IsNot | Compares two Object Variable for Reference, False If the same object reference | |
Like | compares a string against a pattern. |
Example #2: Comparison operators in VB.NET
Module operators
Sub Main()
Dim x As Integer = 10
Dim y As Integer = 12
'Output: x > y is False
Console.WriteLine("x > y is:{0}", x > y)
'Output: x < y is True
Console.WriteLine("x < y is:{0}", x < y)
'Output: x = y is False
Console.WriteLine("x = y is:{0}", x = y)
'Output: x <> y is True
Console.WriteLine("x <> y is:{0}", x <> y)
'Output: x >= y is False
Console.WriteLine("x >= y is:{0}", x >= y)
'Output: x <= y is True
Console.WriteLine("x <= y is:{0}", x <= y)
End Sub
End Module
Output:
3. Logical/Bitwise Operators
The following are the Logical Operators supported by VB.NET. In this case, x and y are Boolean Values.
Logical/Bitwise operators in VB.NET Operators
Operators | Meaning | Example |
And | Logical as well as bitwise AND operator. Returns True If both the operands are true | x And y |
Does not perform short-circuiting, i.e., it evaluates both the expressions | ||
Or | Logical as well as bitwise OR operator. Returns True If any of the two operands is true. It does not perform short-circuiting. | x Or y |
Not | Logical as well as bitwise NOT operator. If True, then this operator will make it false. | Not y |
Xor | Logical as well as bitwise Logical Exclusive OR operator. Returns True if both expressions are the same; otherwise False. | x Xor y |
AndAlso | Logical AND operator. Works only on Boolean data. Performs short-circuiting. | x AndAlso y |
OrElse | Logical OR operator. Works only on Boolean data. Performs short-circuiting. | x OrElse y |
IsFalse | Determines whether an expression is False | |
IsTrue | Determines whether an expression is False |
Example #3: Logical operators in VB.NET
Module operators
Sub Main()
Dim x As Boolean = True
Dim y As Boolean = False
'Output: x and y is False
Console.WriteLine("x And yis:{0}", x And y)
'Output: x or y is True
Console.WriteLine("x or y is:{0}", x Or y)
'Output: not x is False
Console.WriteLine("not y is:{0}", Not y)
End Sub
End Module
Output:
4. Bit Shift Operators
The Bit Shift operators are used to perform shift operations on binary level or values. They perform bit by bit operation. In this case, x and y are numeric Values.
Bit Shift operators in VB.NET
Operators | Meaning | Example |
And | Bitwise AND Operator copies a bit to the result if it exists in both operands. | x And y |
Or | Binary OR Operator copies a bit if it exists in either operand. | x or y |
Xor | Sets bit if any one of the bit is set from both operands. | X xor y |
Not | It toggles every bit of operand. | Not x |
<< | Shifts binary bits by the number of times specified by operand to left. | x << 3 |
>> | Shifts binary bits by the number of times specified by operand to right. | x << 3 |
5. Assignment Operators
Assignment operators are used for assigning values to variables in VB.NET.
Dim x As Integer = 7 is a simple assignment statement that assigns a value on the right side i.e. 7 to variable x. There are operators in VB.NET like x += 4 which have additional meaning. Such operators are known as compound operators. The meaning of x += 4 is equivalent to adding 4 to variable x and then assigning the resultant value back to x.
Assignment operators in VB.NET
Operators | Example | Equivalent to |
= | x = 4 | x = 4 |
+= | x += 4 | x = x + 4 |
-= | x -= 4 | x = x – 4 |
*= | x *= 4 | x = x * 4 |
/= | x /= 4 | x = x / 4 |
\= | x \= 4 | x = x \ 4 |
^= | x ^= 4 | x = x ^ 4 |
<<= | x << = 4 | x = x << 4 |
>>= | x >> = 4 | x = x >> 4 |
&= | x &= 4 | x = x & 4 |
6. Miscellaneous Operators
There are few other important operators supported by VB.NET which are,
Miscellaneous operators in VB.NET
Operators | Example | Equivalent to |
AddressOf | Returns the address of a procedure. | AddHandler Button1.Click, AddressOf Button1_Click |
Await | It is applied to an operand in an asynchronous method or lambda expression to suspend execution of the method until the awaited task completes. | Dim result As res = Await AsyncMethodThatReturnsResult() Await AsyncMethod() |
GetType | It returns a Type object for the specified type. | MsgBox(GetType(Integer).ToString()) |
Function Expression | It declares the parameters and code that define a function lambda expression. | Dim add5 = Function(num As Integer) num + 5 ‘prints 10 Console.WriteLine(add5(5)) |
If | It uses short-circuit evaluation to conditionally return one of two values. | Dim num = 5 Console.WriteLine(If(num >= 0, “Positive”, “Negative”)) |
Recommended Articles
This has been a guide to VB.NET Operators. Here we have discussed different types of VB.NET Operators with examples. You can also go through our other suggested articles to learn more –