Operators in VB.net
By: Steven Holzner
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):
\ Integer division
These are the Assignment operators (for example, temperature = 72 stores the value 72 in the variable temperature):
^= 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
Isâ€”True 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.
1. nice explanation
View Tutorial By: harish at 2012-12-04 06:20:45
2. hi good article with explanation
View Tutorial By: yuganthi at 2012-09-24 05:18:35
Comment on this tutorial
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
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
Exception Filtering in the Catch Block in VB.net
Using Multiple Catch Statements in VB.net
Throwing an Exception in VB.net
Throwing a Custom Exception in VB.net
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net