Operators in VB .Net

Introduction

  • Operators are a very important part of VB .Net Programming.

Definition

  • An operator is a symbol that performs specific mathematical or logical manipulations on a given operands.

Type of Operators in VB .Net

  • VB.Net has rich in built-in operators. These are –
    • Arithmetic Operators
      • Arithmetic operators are used for performing common mathematical operations.
      • These operators are –
Operator Symbol Description Example
+Adds two operandsx + y
Subtracts second operand from the firstx – y
*Multiplies both operandsx * y
/Divides one operand by another and returns a floating-point resultx / y
\Divides one operand by another and returns an integer resultx \ y
MODModulus division that gives remainder as result after an integer divisionx MOD y (remainder of x/y)
^Exponent operator that raises one operand to the power of anotherx ^ y or xy (x to the power y)

    Comparison Operators

    • Comparison operators are basically used to compare different values and normally return a Boolean value in the form of either True or False depending upon the condition verified.
    • These operators are – 

      Operator SymbolDescription Example
      =Equality symbol that checks and returns True if both values are the same.x = y
      <>Non-Equality symbol that checks and returns True if both values are unequal.x < > y
      > Greater than symbol that checks and returns true if the first value is greater than the second valuex > y
      <Less than symbol that checks and returns true if the first value is less than second valuex < y 
      >=Greater than equal to symbol that checks for two conditions and return true, If the first value is greater than or equal to the second value.x >= y
      <=Less than equal to symbol that Checks for two conditions and returns true, If the first value is less than or equal to the second value.x <= y
      IsCompares two object variables for reference and return true if the same object reference.
      IsNotCompares two object variables for reference and return false If the different object reference.
      LikeCompares a string against a specific pattern.

    Logical Operators

    Operator

    Symbols

    DescriptionExample
    AndThe And Operator verifies, whether both the operands are true then returns True.

    (A And B) results = False

    (A And A) results = True

    OrThe Or Operator verifies and returns a true value, if anyone operand is true from both the operands.(A Or B), result = True
    NotThe Not Operator reverses the logical condition such as if the operand’s logic is True then it reveres the condition and makes it false and its vice-versa.

    Not A

    Or

    Not(A And B), result = True

    XorIt is an Exclusive OR Operator that verifies, whether both the expression is true or false, the result is True; otherwise, the result is False.A XOR B is True
    AndAlsoIt is a logical AND Operator that performs short-circuit operation on the variables, and if both the operands are true, the result is True else the result is False.A AndAlso B = False
    OrElseIt is a logical OR Operator that perform short-circuit operation on Boolean data. If anyone of the operand is true, the result is True else the result is False.A OrElse B = True
    IsFalseThe IsFalse Operator is used to determine that an expression is False.
    IsTrueThe IsTrue Operator is used to determine that an expression is True.

    Bitwise Operator

    Operator SymbolsDescriptionExample
    ANDThe Binary AND Operator are used to copy the common binary bit in the result if the bit exists in both operands.
    ORThe Binary OR Operator is used to copy a common binary bit in the result if the bit found in either operand.
    XORThe Binary XOR Operator in VB.NET is used to determine whether a bit is available to copy in one operand instead of both.
    NotThe 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.
    << [Left Shift]The Binary Left Shift Operator is used to shift the bit to the left side in their structure.
    >> [Right Shift]The Binary Right Shift Operator is used to shift the bit to the right side in their structure.

    Assignment Operator

    OperatorDescriptionExample
    =It is a simple Assignment Operator used to assign a right-side operand or value to a left side operand.X = 7, here X assigns a value 7.
    X = P + Q, here (P + Q) variables or value assign to X.
    +=It is an Add and Assignment Operator that is used to add the value of the right operands first and then finally assigns to the left operand. X += 7, which is equivalent to
    X= X+7 (i.e., 7 is added and assign to X and then result saved to Left X operand finally)
    -=It is a Subtract and Assignment Operator, which subtracts the right-side operands or values first and then assigns to the left operand finally.X -= 10, which is equivalent to X = X – 10
    *=It is a Multiply and Assignment Operator, which multiplies the right-side operands or values first and then assign to the left operand finally. X *= Y, which is equivalent as X = X * Y
    /=It is an Integer Divide and Assignment Operator, which divides the right operands or values first and then, the result is assigned to the left operand finally.X /= Y, which is equivalent as X = X/ Y
    \=It is a Floating- Point Divide and Assignment Operator, which divides the right-side operands or values and then the result is finally assigned to the left side operand.X \= Y, which is same as X = X \ Y
    ^=It is an Exponent and Assignment Operator, which calculates the right-side operands or values as power or exponent and then, the result is finally assigned to the left operand.X ^= Y, which is same as X = X ^ Y
    &=It is a Concatenate String Assignment Operator which is used to bind/concatenate the right-side strings or variables and then, the result is assigned finally to the left operand.

    Str &= name, which is equivalent as

    Str = Str & name

    Concatenation Operator

    OperatorDescriptionExample
    &This symbol is used to bind two or more operands together. Using this, a non-string operand can also be concatenated/bind with a string variable (when Option Strict is on).Str= Wel & come,
    Str= Welcome
    +This symbol is also used to add or concatenate two number or strings.Str= Wel + come,
    Str= Welcome

    Miscellaneous Operator

    Name of OperatorsDescriptionExample
    AwaitThis operator is used in an operand to suspend the execution of an asynchronous method or lambda expression until the awaited task completes.Dim output as out = Await AsyncMethodThatReturnsResult() Await AsyncMethod()
    AddressOfThe AddressOf Operator is used to provide a reference to the address of a procedure.AddHandler Button1.Click, AddressOf Button1_Click
    GetTypeThis operator is used to retrieve the data type of the specified object. In addition, the retrieved object type provides various information such as methods, properties, and events.MsgBox(GetType(String).ToString())
    Function ExpressionIt defines the lambda expression, which declares the parameter and code. [A Lambda expression is a function that is used to calculate and return value without defining the name.]Dim mult = Function(num As Integer) num * 4
    Console.WriteLine(mult(4))

    Order of Precedence in VB .Net Operators

    • Operator precedence determines the order in which different Operators in a complex expression are evaluated.
    • There are different and distinct levels of precedence of operators.
    • In the Operator Precedence table, the operators occupy at a higher level of precedence are evaluated first. But operators having similar precedents in same levels are evaluated at either from left-to-right or right-to-left direction following associativity rules.
    Name of OperationsOperator SymbolsOrder of Precedence
    AwaitHighest
    Exponential^
    Unary identity and negation+, –
    Multiplication and floating-point division*, /
    Integer division\
    Modulus divisionMod
    Addition and Subtraction+, –
    Arithmetic bit shift<<, >>
    All comparison Operators=, <>, <, <=, >, >=, Is, IsNot, Like, TypeOf …is
    NegationNot
    ConjunctionAnd, AndAlso
    Inclusive disjunctionOr, Else
    Exclusive disjunctionXorLowest

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.