VB.NET Operators

This article covers the use of VB.NET operators.

Along with VB.NET Data types, Operators form the very base of the VB.NET language. These two topics are the first things anyone coming into this language should learn. Operators are used widely in numerous applications, along with numerous other statements such as loops. The most commonly used operators or Arithmetic, Assignment and comparison.


Arithmetic Operators

Arithmetic Operators in VB.NET are used to perform numerical calculations. Below are all the Arithmetic operations supported by VB.NET.

Name Operator Description
Addition+ Sum of two numbers.
Subtraction Difference of two numbers.
Multiplication* Product of two numbers.
Division/ Division of two numbers, returns float value.
Division\Division of two numbers, returns integer value. (Decimal part removed)
ModulusMOD Remainder of the division of two numbers.
Exponent^ Exponential of a number.

Comparison Operators

Comparison operators are used between two values or statements to determine a result. Below is the list of all the supported Comparison operations in VB.NET

NameOperatorDescription
Equals to = Returns True if values are equal
Not Equal to <>Returns True if values are not equal
Greater than >Returns True is value is greater
Greater than or equals to >=Returns True is value is equal or greater
Less than: <Returns True is value is smaller
Less than: <=Returns True if value is equal or smaller

Assignment Operators

Arguably the most common, Assignment Operators are used to assign values to variables. See the full list of Assignment operations below.

Operator Example Equivalent
= x = 2 N/A
+=x += 2x = x + 2
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
\=x \= 2x = x \ 2
%=x %= 2x = x % 3
^=x ^= 2x = x ^ 2

Logical Operators

Used to carry out logical operations on and between values in VB.NET such as and and or. Below is a full list of logical operators in VB.NET.

OperatorDescriptionExample
AndReturns True is both expressions are True(x < 5) And (y < 2)
OrReturns True if one or both of two expressions is True(x = 5) Or (x = 2)
NotAn inverting operator. Converts True to False and vice versaNot(5 > 3)
XorReturns True only if one of two expressions are True. (x = 5) Xor (x = 2)
AndAlso Returns True is both expressions are True (x < 5) And (y < 2)
OrElse Returns True if one or both of two expressions is True (x = 5) Or (x = 2)
IsFalseDetermines whether a given expression is True
IsTrue Determines whether a given expression is False

AndAlso and OrElse are almost identical to And and Or respectively. The difference is that And and Or can also be used as Bit-wise operators, whereas AndAlso and OrElse can only be used on Boolean Data. This distinction helps to avoid confusion in some cases.


Bit-wise Operators

Bit-Wise operations occur between binary numbers. The operators operate on each bit in the numbers individually, producing a single bit as output until all the bits have been acted upon.

OperatorDescription
AndIf both bits are one, the output bit is one as well.
OrIf either of the bits or both is one, the output bit is one.
XorIf one of the bits is one, but not both, the output bit is one.
NotActs on a single binary number. Flips the bit from 1 to 0 and vice versa.
<< Binary Left Shift Operator. The bits of the binary number on the left hand side of this operators are shifted to the left by the number specified by the right hand operand.
>> Binary Right Shift Operator. The bits of the binary number on the left hand side of this operators are shifted to the right by the number specified by the right hand operand.

Operators that act on a single number/value are called unary operators.


This marks the end of the VB.NET operators Article. Any suggestions or contributions for our site CodersLegacy are more than welcome. Any relevant questions can be asked below in the comments section.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments