Operators in VB.net

By: Steven Holzner Emailed: 1697 times Printed: 2203 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Visual Basic comes with plenty of built-in operators, which let you manipulate your data. For example, here I'm adding the values in intVariable1 and intVariable2 with the addition operator, +, and storing the result in intVariable3 with the assignment operator, =:

Module Module1
    Sub Main()
        Dim intVariable1 As Integer = 1234
        Dim intVariable2 As Integer = 2345
        Dim intVariable3 As Integer
        intVariable3 = intVariable1 + intVariable2
        System.Console.WriteLine(intVariable3)
    End Sub
End Module

This code prints out the result of adding 1234 and 2345, which is 3579. An operator works on operands; for example, in the expression 5 + 4, 5 is operand1,+ is the operator, and 4 is operand2. Some operators in Visual Basic take two operands, and some take one.

There are various types of operators in Visual Basic, and I'll go over them all here. Here are the Arithmetic operators (for example, the expression 5 + 4 yields a value of 9):

  • ^ Exponentiation

  • * Multiplication

  • / Division

  • \ Integer division

  • Mod Modulus

  • + Addition

  • - Subtraction

These are the Assignment operators (for example, temperature = 72 stores the value 72 in the variable temperature):

  • = Assignment

  • ^= Exponentiation followed by assignment

  • *= Multiplication followed by assignment

  • /= Division followed by assignment

  • \= Integer division followed by assignment

  • += Addition followed by assignment

  • -= Subtraction followed by assignment

  • &= Concatenation followed by assignment

Here are the Comparison operators (these values yield true or false values—for example, 5 > 4 yields a value of True):

  • < (Less than)—True if operand1 is less than operand2

  • <= (Less than or equal to)—True if operand1 is less than or equal to operand2

  • > (Greater than)—True if operand1 is greater than operand2

  • >= (Greater than or equal to)—True if operand1 is greater than or equal to operand2

  • = (Equal to)—True if operand1 equals operand2

  • <> (Not equal to)—True if operand1 is not equal to operand2

  • IsTrue if two object references refer to the same object

  • Like—Performs string pattern matching

These are the String Concatenation operators (for example, "Hi "& " there " yields the string "Hi there".):

  • & String concatenation

  • + String concatenation

These are the Logical/Bitwise operators, where bitwise means working bit by bit with numerical values. These types of operators can work on logical values (for example, if blnValue1 is set to True and blnValue2 is set to False, then blnValue1 Or blnValue2 returns a value of True) or numbers for bitwise operations, which work on their operands bit by bit (for example, if intValue1 is set to 2 and intValue2 is set to 1, then intValue1 Or intValue2 yields 3):

  • And— Performs an And operation (for logical operations: True if both operands are True, False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True).

  • Not— Reverses the logical value of its operand, from True to False and False to True, for bitwise operations, turns 0 into 1 and 1 into 0.

  • Or— Operator performs an Or operation (for logical operations: True if either operand is True, False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True).

  • Xor— Operator performs an exclusive-Or operation (for logical operations: True if either operand, but not both, is True, and False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True).

  • AndAlso— Operator A "short circuited" And operator; if the first operand is False, the second operand is not tested.

  • OrElse— Operator A "short circuited" Or operator, if the first operand is True, the second is not tested.

And here are two other miscellaneous operators:

  • AddressOf— Gets the address of a procedure.

  • GetType— Gets information about a type.

VB6 programmers will note a whole new set of assignment operators, such as +=, -=, and so on. Following the lead of languages like Java, VB .NET now supports these combination operators. For example, += is a combination of + and =, which means that you can write intValue1 = intValue1 + 1 asintValue1 += 1. In a similar way, you can write intValue1 = intValue1 * 5 asintValue1 *= 5, providing an easy shortcut.

Also, in Visual Basic .NET, if the first operand of an And operator evaluates to False, the remainder of the logical expression is not evaluated. Similarly, if the first operand of an Or operator evaluates to True, the remainder of the logical expression is not evaluated. This is called short-circuiting.


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

hi good article with explanation

View Tutorial          By: yuganthi at 2012-09-24 05:18:35
2. View Comment

nice explanation

View Tutorial          By: harish at 2012-12-04 06:20:45

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Nested If and Single line if statement
What is .NET Framework and the Common Language Runtime?
Update cell data in an Excel file using OLEDB in VB.net
Your first VB.NET Crystal Reports - A step by step guide
Visual Basic Statements
Operators in VB.net
If…Else Statements in VB.net
Using Select Case in VB.net
Do Loop in VB.net
Sub Procedures and Functions in VB.net
Name Value Collecion in VB.net
String Insert, index off & format in VB.net
String.Contains (), compare, clone in VB.net
String endswith, concat in VB.net
Vb.net text reader
Most Emailed Articles (in VB.net)
How to export from database to excel using VB.net
Changes in Controls from VB6 to VB.net
What is .NET Framework and the Common Language Runtime?
While Loop in VB.net
Substring in Vb.Net String Class
Create an Excel 2007 file in VB.NET 2005
Insert Pictures in Excel 2007 from VB.NET 2005
Socket Programming in VB.net
Chat Server in VB.net
Using the ServiceBase Class in VB.net
Using Select Case in VB.net
String Insert, index off & format in VB.net
String Split function in VB.net
Vb.net text reader
Format Excel 2007 Page in VB.NET