Operators in VB.net

By Steven Holzner Viewed: 31746 times Emailed: 183 times Printed: 286 times Bookmark and Share



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.




Comments(0)


Be the first one to add a comment

Your name (required):


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


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2009-03-22]Handling Timer Events - and Creating an Alarm Clock in VB.net
[2009-03-22]Creating Menus in Code using VB.net
[2009-03-22]Creating Context Menus in Code using VB.net
[2009-03-22]Creating Tree Views in Code using VB.net
[2009-03-22]Creating List Views in Code using VB.net
[2009-03-16]Send SMS using VB code
[2009-02-27]Creating a Windows Service in VB.net
[2009-02-27]Creating a Windows Service Installer in VB.net
[2009-02-24]OleDbConnection class in VB.net
[2009-02-24]OleDbDataAdapter class in VB.net
[2009-02-24]DataSet Class in VB.net
[2009-02-24]DataTable Class in VB.net
[2009-02-24]DataRow Class in VB.net
[2009-02-22]Chat Server in VB.net
[2009-02-22]A tutorial on Chat Server and Chat Client in VB.net

More Latest News

Most Viewed Articles (in last 30 days)
For Loop in VB.net
Send SMS using VB code
While Loop in VB.net
Your first VB.NET Crystal Reports - A step by step guide
The Select Case statement in VB.net
Using Select Case in VB.net
Sub Procedures and Functions in VB.net
How to export from DataGridView to excel using VB.net
Arrays and Dynamic Arrays in VB.net
If…Else Statements in VB.net
Insert cell data in an Excel file using OLEDB in VB.net
Client Socket Program sample in VB.net
Handling Timer Events - and Creating an Alarm Clock in VB.net
Chat Server in VB.net
How to send email using VB.NET code
Most Emailed Articles (in last 30 days)
For Loop in VB.net
If…Else Statements in VB.net
Send SMS using VB code
While Loop in VB.net
The Option and Imports Statements in VB .NET
What's New in VB .NET? A comparison of VB vs VB.net
Visual Basic Statements
Using Select Case in VB.net
For Each…Next Loop in VB.net
The Select Case statement in VB.net
Do Loop in VB.net
“Using If with And” and Comparing two integers using If
What is .NET Framework and the Common Language Runtime?
Data types in VB.net
Arrays and Dynamic Arrays in VB.net