VB.NET Operator

An Operator performs operations on data value stored in variable, constants and array etc.

  • Operators take one or more operand as input and give a result after applying operation over them. Like in "a+b", 'a' and 'b' are the operands and '+' is an operator which performs the add operation on 'a' and 'b' operands.
  • VB.NET provides rich set of operators which are as follows:
    • Arithmetic Operators
    • Comparison Operators
    • Logical Operators
    • Assignment Operators
    • Bitwise Operators
    • Conditional Operator

Arithmetic Operators


Arithmetic operators are the operators which perform the mathematical operation between two operands or it can be more than two operands.

  • We can classified the Arithmetic Operators into two categories:
    • Binary Operators
    • Unary Operators
Binary Operators
  • Binary Operators are those arithmetic operators which take two or more operands to perform operation.
  • Here is the list of Binary operators supported by the VB.NET:
  • suppose a=7, b=7
OperatorDescriptionExample

^

It is used to calculate the power to another operand.a^2 = 49

+

It is used to add two or more operandsA+b = 14

-

It is used to subtract second operand from the firsta-b = 0

*

It is used to multiplies both operandsa*b = 49

/

It is used to divide one operand by another and it returns a floating point as resulta/b = 1

\

It is used to divide one operand by another and it returns an integer as resulta\b = 1

MOD

It is known as Modulus Operator and it provides remainder of after an integer division.a MOD b = 0
Unary Operators
  • Unary Operators are those operator which takes only one operand to perform operation.
  • Here is the list of the Unary Arithmetic operators supported by the VB.NET language:

Here is an example as follows:

Module Module1

Sub Main()
Dim a As Integer = 7
Dim b As Integer = 7
Dim p As Integer = 2
Dim c As Integer
Dim d As Single
c = a + b
Console.WriteLine("Value of c is {0}", c)
c = a - b
Console.WriteLine(" Value of c is {0}", c)
c = a * b
Console.WriteLine("Value of c is {0}", c)
d = a / b
Console.WriteLine(" Value of d is {0}", d)
c = a \ b
Console.WriteLine(" Value of c is {0}", c)
c = a Mod b
Console.WriteLine("Value of c is {0}", c)
c = b ^ p
Console.WriteLine(" Value of c is {0}", c)
Console.ReadLine()


End Sub

End Module

The output is as follows:

Comparison Operators


Comparison operators are used for the comparison between two values.


  • Here is table containing the list of several comparison operators:
  • let a=5 and 4
    Operator Description Example
    = It is used to check if the values of two operands are equal or not; if yes, then condition becomes true. a=b return false
    <> It is used to check if the values of two operands are equal or not; if values are not equal, then condition becomes true. a<>b returns true
    > It is used to checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true. a>b returns true
    < It is used to check if the value of left operand is less than the value of right operand; if yes, then condition becomes true. a<b returns false
    >= It is used to check if the value of left operand is greater than or equal to the value of right operand; if yes, then condition becomes true. a>=b returns true
    <= It is used to check if the value of left operand is less than or equal to the value of right operand; if yes, then condition becomes true. a<=b returns false

    VB.NET provides some special operators as follows:

  • Is : it is used to compare two objects reference variable references and it tells if both objects references refer to the same object and it is not performing object comparisons. If first object and 2nd object refer the exact same object instance then it will return true otherwise false.
  • IsNot: it is used to compare two objects reference variable references and it tells if both objects references refer to the different objects. If first object and 2nd object refer the exact same object instance then it will return true otherwise false.
  • Like: it is used for checking the pattern string.
  • The example is shown below:

    Module Module1

    Sub Main()

    Dim a As Integer = 7
    Dim b As Integer = 10
    If (a = b) Then
    Console.WriteLine("a is equal to b")
    Else
    Console.WriteLine("a is not equal to b")
    End If
    If (a < b) Then
    Console.WriteLine("a is less than b")
    Else
    Console.WriteLine("a is not less than b")
    End If
    If (a > b) Then
    Console.WriteLine("a is greater than b")
    Else
    Console.WriteLine("a is not greater than b")
    End If
    ' change value of a and b
    a = 5
    b = 20
    If (a <= b) Then
    Console.WriteLine("a is less than or equal to b")
    End If
    If (b >= a) Then
    Console.WriteLine("b is greater than or equal to b")
    End If
    Console.ReadLine()
    End Sub


    End Module

    The output is as below:

    Logical Operators


    • Logical operators check the logical relationship between the two expressions or expressions can be more than two.
    • Here is the table containing the list of several Logical Operators:
    Operator Description
    And It is the logical or bitwise AND operator. If both the operands are true, then condition becomes true.
    Or It is the logical or bitwise OR operator. If any of the two operands is true, then condition becomes true.
    Not It is the logical or bitwise NOT operator. It is used to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
    Xor It is the logical or bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise it returns False.
    AndAlso This is logical AND operator. It works only on Boolean value. It is used to perform short-circuiting.
    OrElse This is the logical OR operator. It works only on Boolean value. It is used to perform short-circuiting.
    IsFalse It is used to determine whether an expression is False.
    IsTrue It is used to determine whether an expression is True.

    the example is shown below:

    Module Module1

    Sub Main()
    Dim a As Boolean = False
    Dim b As Boolean = True
    Dim c As Integer = 4
    Dim d As Integer = 10
    'logical And, Or and Xor Checking
    If (a And b) Then
    Console.WriteLine("Condition is true")
    End If
    If (a Or b) Then
    Console.WriteLine("Condition is true")
    End If
    If (a Xor b) Then
    Console.WriteLine("Condition is true")
    End If
    'bitwise And, Or and Xor Checking
    If (c And d) Then
    Console.WriteLine("Condition is true")
    End If
    If (c Or d) Then
    Console.WriteLine("Condition is true")
    End If
    If (c Or d) Then
    Console.WriteLine("Condition is true")
    End If
    'Only logical operators
    If (a AndAlso b) Then
    Console.WriteLine("Condition is true")
    End If
    If (a OrElse b) Then
    Console.WriteLine("Condition is true")
    End If

    ' lets change the value of a and b
    a = False
    b = True
    If (a And b) Then
    Console.WriteLine("Condition is true")
    Else
    Console.WriteLine("Condition is not true")
    End If
    If (Not (a And b)) Then
    Console.WriteLine("Condition is true")
    End If
    Console.ReadLine()
    End Sub

    End Module

    The output is as follows:

    Assignment Operators


    Assignment operators are used to assign the value in variables, arrays or constant. It is denoted by = symbol.

    • Here is a list of Assignment operators in the given table:
    • Operator Description
      = It is the logical or bitwise AND operator. If both the operands are true, then condition becomes true.
      += It is the logical or bitwise OR operator. If any of the two operands is true, then condition becomes true.
      -= It is the logical or bitwise NOT operator. It is used to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
      *= It is the logical or bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise it returns False.
      /= This is logical AND operator. It works only on Boolean value. It is used to perform short-circuiting.
      \= This is the logical OR operator. It works only on Boolean value. It is used to perform short-circuiting.
      ^= It is called as Exponentiation and assignment operator. First It raises the left operand to the power of the right operand and assigns the result to left operand means it is used to calculate the power.

      the example is shown below:


      Module Module1

      Sub Main()
      Dim a As Integer = 21
      Dim pow As Integer = 2
      Dim str1 As String = "Hello! "
      Dim str2 As String = "eitworld.com"
      Dim c As Integer
      c = a
      Console.WriteLine("Value of c = {0}", c)
      c += a
      Console.WriteLine(" Value of c = {0}", c)
      c -= a
      Console.WriteLine("Value of c = {0}", c)
      c *= a
      Console.WriteLine("Value of c = {0}", c)
      c /= a
      Console.WriteLine("Value of c = {0}", c)
      c = 20
      c ^= pow
      Console.WriteLine("Value of c = {0}", c)

      str1 &= str2
      Console.WriteLine("Value of str1 = {0}", str1)
      Console.ReadLine()
      End Sub

      End Module

      The output is shown below:


      Bitwise Operators


      • Bitwise operators are used to perform operation on binary representation of numeric values.
      • Here is the list of Bitwise operators in the given table:
      • Let us discuss the operation performed by Bitwise operators on the value one by one:
      1) Right Shift Operator (>>)
      • Let us take a value a=8 then what would be the result of a>>2? We have a value a=8 and binary representation of numeric value 8 is as follows:
      • 0000000000001000
      • The value of variable a is shifted by 2 bit right side so the result would be as follows:
      • 0000000000000010
      • It is the binary representation of the numeric value 2 so the result of a>>2 would be=2
      2) Left Shift Operator (<<)
      • Let us take a value a=8 then what would be the result of a<<2? We have a value a=8 and binary Representation of numeric value 8 is as follows:
      • 0000000000001000
      • The value of variable a is shifted by 2 bit left side so the result would be as follows:
      • 0000000000100000
      • It is the binary representation of the numeric value 32 so the result of a>>2 would be=32
      • Before discussing about other bitwise operators let us see their truth tables:
      • Here is a truth table for ^, &, | operators:
      • Truth table of ~ operator is as follows:
      3)Bitwise XOR operator (^)
      • Let us take two values a=8 and b=4 then what would be the result of a^b? 
      • Binary representation of a=8 is as follows:
      • 0000000000001000
      • Binary representation of b=4 is as follows:
      • 0000000000000100
      • Now a^b will give the result as follow:
      • 0000000000001100
      • It is the binary representation of the numeric value 12 so the result of a^b would be=12
      4) Bitwise OR operator (I)
      • Let us take two values a=8 and b=4 then what would be the result of a|b? 
      • Binary representation of a=8 is as follows:
      • 0000000000001000
      • Binary representation of b=4 is as follows:
      • 0000000000000100
      • Now a|b will give the result as follow:
      • 0000000000001100
      • It is the binary representation of the numeric value 12 so the result of a|b would be=12
      5) Bitwise AND operator (&)
      • Let us take two values a=8 and b=4 then what would be the result of a&b? 
      • Binary representation of a=8 is as follows:
      • 0000000000001000
      • Binary representation of b=4 is as follows:
      • 0000000000000100
      • Now a&b will give the result as follow:
      • 0000000000000000
      • It is the binary representation of the numeric value 0 so the result of a&b would be=0.
      6) Bitwise One's Complement operator (~)
      • Let us take a value c=60 then what would be the result of ~c? 
      • Binary representation of c=60 is as follows:
      • 00111100
      • Now ~a will give the result as follow:
      • 11000011
      • It is the binary representation of the numeric value -61 so the result of ~c would be=-61.

      The example is shown below:


      Module Module1

      Sub Main()
      Dim a As Integer = 6
      Dim b As Integer = 13
      Dim c As Integer = 0
      c = a And b
      Console.WriteLine(" Value of c is {0}", c)
      c = a Or b
      Console.WriteLine("Value of c is {0}", c)
      c = a Xor b
      Console.WriteLine("Value of c is {0}", c)
      c = Not a
      Console.WriteLine("Value of c is {0}", c)
      c = a << 2
      Console.WriteLine("Value of c is {0}", c)
      c = a >> 2
      Console.WriteLine("Value of c is {0}", c)
      Console.ReadLine()
      End Sub

      End Module

      The output is shown below: Miscellaneous Operators: VB.NET provides some operators which are as follows: AddressOf : it is used to return the address of the procedure. Await: This operator is applied to an operand in an asynchronous method or lambda expression to suspend the execution of the method till the awaited task completes.