VB.net Operators
VB.net Operators
NET Operators
In VB.NET programming, the Operator is a symbol that is used to perform various operations on
variables. VB.NET has different types of Operators that help in performing logical and mathematical operations on
data values. The Operator precedence is used to determine the execution order of different Operators in the VB.NET
programming language.
3+2-1
The symbol + and - are the Operators, and the 3, 2, and 1 are operands.
o Arithmetic Operators
o Comparison Operators
o Logical and Bitwise Operators
o Bit Shift Operators
o Assignment Operators
o Concatenation Operators
o Miscellaneous Operators
Arithmetic Operators
The Arithmetic Operators in VB.NET, used to perform mathematical operations such as subtraction, addition,
multiplication, division, etc. on the operands in VB.NET. These are as follows:
Arithmetic_Operator.vb
Imports System
Module Arithmetic_Operator
Sub Main()
'Declare a, b And c as integer Data Type()
Dim a, b, c As Integer
Dim d As Single
a = 17
b=4
' Use of + Operator
c=a+b
Console.WriteLine(" Sum of a + b is {0}", c)
'Use of - Operator
c=a-b
Console.WriteLine(" Subtraction of a - b is {0}", c)
'Use of * Operator
c=a*b
Console.WriteLine(" Multiplication of a * b is {0}", c)
'Use of / Operator
d=a/b
Console.WriteLine(" Division of a / b is {0}", d)
'Use of \ Operator
c=a\b
Console.WriteLine(" Similar to division Operator (return only integer value) of a - b is {0}", c)
'Use of ^ Operator
c=a^b
Console.WriteLine(" Power of a ^ b is {0}", c)
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program, by pressing the F5 button or Start button from the Visual Studio; then it
shows the following result:
Comparison Operators
As the name suggests, the Comparison Operator is used to compare the value of two variables or operands for the
various condition such as greater, less than or equal, etc. and returns a Boolean value either true or false based on the
condition.
<> It is a Non-Equality Operator that checks whether the value of (A <> B), check Non-
the two operands is not equal; it returns true; otherwise, it Equality
shows false.
> A greater than symbol or Operator is used to determine (A > B); if yes, TRUE,
whether the value of the left operand is greater than the
value of the right operand; If the condition is true, it returns Else FALSE
TRUE; otherwise, it shows FALSE value.
< It is a less than symbol which checks whether the value of the (A < B); if the condition is
left operand is less than the value of the right operand; If the true, returns TRUE else
condition is true, it returns TRUE; otherwise, it shows FALSE FALSE
value.
<= This symbol represents less than equal to which determines A <= B
the first operand is less than or equal to the second operand,
and if the condition is true, it returns TRUE; otherwise, it
shows FALSE.
Is The Is Operator is used to validate whether the two objects result = obj1 Is obj2
reference the same variable or object; If the test is true, it
returns True; otherwise, the result is False. In short, it checks
the equality of the objects. An Is Operator is also used to
determine whether the object refers to a valid object.
IsNot The IsNot Operator is similar to Is Operator, except that the Result = obj1 IsNot obj2
two object references the different object; if yes, the result is
True; otherwise, the result is False.
Like The Like Operator is used to check the pattern expression of result = string Like the
string variable; And if the pattern matched, the result is True; pattern, the pattern
otherwise, it returns False. represents the series of
characters used by Like
Operator.
Comparison_Operator.vb
Imports System
Module Comparison_Operator
Sub Main()
'declaration of Integer, Object and String Data Type variables
Dim x As Integer = 5
Dim y As Integer = 10
Dim Result, obj, obj2 As Object
Dim str, str2 As String
str = "Apple12345"
str2 = "Apple12345"
obj = 10
obj2 = 20
'Use of = Operator
Console.WriteLine(" Output of x = y is {0}", x = y)
'Use of Is Operator
Result = obj Is obj2
Console.WriteLine(" Output of obj Is obj2 is {0}", Result)
'Use of Is Operator
Result = obj IsNot obj2
Console.WriteLine(" Output of obj IsNot obj2 is {0}", Result)
Now compile and execute the above code by pressing the F5 button or Start button in Visual studio, it returns the
following output:
Logical and Bitwise Operators
The logical and bitwise Operators work with Boolean (true or false) conditions, and if the conditions become true, it
returns a Boolean value. The following are the logical and bitwise Operators used to perform the various logical
operations such as And, Or, Not, etc. on the operands (variables). Suppose there are two operand A and B, where A is
True, and B is False.
And The And Operator represents, whether both the operands are true; the (A And B),
result is True. result = False
Or It is an Or Operator that returns a true value; if anyone operand is true (A Or B), result
from both the operands. = True
Not The Not Operator is used to reverse the logical condition. For example, Not A
if the operand's logic is True, it reveres the condition and makes it False.
Or
Not(A And B) is
True
Xor It is an Exclusive OR Operator that represents, whether both the A Xor B is True
expression is true or false, the result is True; otherwise, the result is
False.
AndAlso It is a logical AND Operator that performs short-circuit operation on the A AndAlso B =
variables, and if both the operands are true, the result is True else the False
result is False.
Logic_Bitwise.vb
Imports System
Module Logic_Bitwise
Sub Main()
Dim A As Boolean = True
Dim B As Boolean = False
Dim c, d As Integer
c = 10
d = 20
'Use of Or Operator
If A Or B Then
Console.WriteLine(" Operands A Or B are True")
End If
'Use of Or Operator
If c Or d Then
Console.WriteLine(" Operands c Or d is True")
End If
Now compile and execute the above code by pressing the F5 button or Start button in Visual studio, it returns the
following output:
Operator Description
AND The Binary AND Operator are used to copy the common binary bit in the result if the bit
exists in both operands.
OR The Binary OR Operator is used to copy a common binary bit in the result if the bit found
in either operand.
XOR The Binary XOR Operator in VB.NET, used to determine whether a bit is available to copy in
one operand instead of both.
Not The binary NOT Operator is also known as the binary Ones' Compliment Operator, which is
used to flip binary bits. This means it converts the bits from 0 to 1 or 1 to 0 binary bits.
<< The Binary Left Shift Operator is used to shift the bit to the left side.
>> The Binary Right Shift Operator is used to shift the bit to the right side.
BitShift_Operator.vb
Imports System
Module Bitshift_Operator
Sub Main()
Dim x, y, z As Integer
x = 12
y = 25
Dim a, b As Double
a = 5 ' a = 5(00000101)
b = 9 ' b = 9(00001001)
'Use of Or Operator
z = x Or y
Console.WriteLine(" BitShift Operator x Or y is {0}", z)
z = x Xor y
Console.WriteLine(" BitShift Operator x Xor y is {0}", z)
z = Not y
Console.WriteLine(" BitShift Operator Not y is {0}", z)
'Output is 00010010
Console.WriteLine(" Bitwise Left Shift Operator - b<<1 = {0}", b << 1)
'Output is 00000100
Console.WriteLine(" Bitwise Right Shift Operator - b>>1 = {0}", a << 1)
Now compile and execute the above code by pressing the F5 button or Start button in Visual studio, it returns the
following output:
Assignment Operators
The Assignment Operators are used to assign the value to variables in VB.NET.
+= An Add AND assignment Operator is used to add the value X += 5, which means
of the right operand to the left operand. And the result is X= X+5 ( 5 will add and
assigned to the left operand. assign to X and then result
saved to Left X operand)
&= It is a concatenate string assignment Operator used to bind Str &= name, which is same
the right-hand string or variable with the left-hand string or as Str = Str & name
variable. And then, the result will be assigned to the left
operand.
Assign_Operator.vb
Imports System
Module Assign_Operator
Sub Main()
'Declare variable and b As Integer
Dim A As Integer = 5
Dim B As Integer
Dim Str, name As String
name = "come"
Str = "Wel"
'Use of = Operator
B=A
Console.WriteLine(" Assign value A to B is {0}", B)
'Use of += Operator
B += A
Console.WriteLine(" Output of B += A is {0}", B)
'Use of -= Operator
B -= A
Console.WriteLine(" Output of B -= A is {0}", B)
'Use of *= Operator
B *= A
Console.WriteLine(" Output of B *= A is {0}", B)
'Use of /= Operator
B /= A
Console.WriteLine(" Output of B /= A is {0}", B)
'Use of = Operator
B \= A
Console.WriteLine(" Output of B \= A is {0}", B)
'Use of ^= Operator
B ^= A
Console.WriteLine(" Output of B ^= A is {0}", B)
Now compile and execute the above code by pressing the F5 button or Start button in Visual studio, it returns the
following output:
Concatenation Operators
In VB.NET, there are two concatenation Operators to bind the operands:
& It is an ampersand symbol that is used to bind two or more operand Result = Wel
together. Furthermore, a nonstring operand can also be concatenated & come,
with a string variable ( but in that case, Option Strict is on). Result =
Welcome
MyProgram.vb
Imports System
Module MyProgram
Sub Main()
Dim str As String = "Wel"
Dim str2 As String = "come"
Dim str3 As String = " "
Dim str4 As String = "to JavatPoint"
Dim result As String
Dim result2 As String
result = str & str2
Console.WriteLine(" Result = str & str2 gives = {0}", result)
result2 = str + str2 + str3 + str4
Console.WriteLine(" Result = str + str2 + str3 +str4 gives = {0}", result2.ToString)
Console.ReadLine()
End Sub
End Module
Now compile and execute the above code by pressing the F5 button or Start button in Visual studio, it returns the
following output:
Miscellaneous Operators
There are some important Operator in VB.NET
Function It defines the lambda expression, which Dim mul2 = Function(num As Integer) num
Expression declares the parameter and code. A * 4
Lambda expression is a function that is Console.WriteLine(mul2(4))
used to calculate and return value without
defining the name.
Misc_Operator.vb
Imports System
Module Misc_Operator
Sub Main()
' Initialize a variable
Dim a As Integer = 50
' GetType of the Defined Type
Console.WriteLine(GetType(Double).ToString())
Console.WriteLine(GetType(Integer).ToString())
Console.WriteLine(GetType(String).ToString())
Console.WriteLine(GetType(Single).ToString())
Console.WriteLine(GetType(Decimal).ToString())
'Use of Function()
Dim multiplywith10 = Function(sum As Integer) sum * 10
Console.WriteLine(multiplywith10(10))
Console.WriteLine(If(a >= 0, "Negative", "Positive"))
End Sub
End Module
Now compile and execute the above code by pressing the F5 button or Start button in Visual studio, it returns the
following output: