Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Language features - Operators

Total Hit ( 1973)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


In this article we will see all operators supported by VB.net

Here is the list of operators categories

Category Operators Description
Arithmetic Operators ^, *, /, \, Mod, +, - These operators perform mathematical calculations.
Assignment Operators =, ^=, *=, /=, \=, +=, -=, <<=, >>=, &= These operators perform assignment operations.
Comparison Operators Is, Like, < (Less than), <= (Less than or equal to), > (Greater than), >= (Greater than or equal to), = (Equal to), <> (Not equal to) These operators perform comparisons.
Concatenation Operators &, + These operators combine strings.
Logical/Bitwise Operators And, Not, Or, Xor, AndAlso, OrElse These operators perform logical operations.
Bit Shift Operators <<, >> These operators perform arithmetic shifts on bit patterns.
Miscellaneous Operators AddressOf, GetType These operators perform miscellaneous operations.

Arithmetic Operators

These operators used to perform many familiar arithmetic operations that involve calculation of numeric values represented by literals, variables, other expressions, function and property calls, and constants.

^ Operator

Raises a number to the power of another number.

Click here to copy the following block
Dim myValue As Double
myValue = 2 ^ 2  ' Returns 4.
myValue = 3 ^ 3 ^ 3  ' Returns 19683.
myValue = (-5) ^ 3  ' Returns -125.
myValue = (-5) ^ 4  ' Returns 625.

* Operator

Multiplies two numbers.

Click here to copy the following block
Dim myValue As Double
myValue = 2 * 2  ' Returns 4.
myValue = 459.35 * 334.90  ' Returns 153836.315.

/ Operator

Divides two numbers and returns a floating-point result.

Click here to copy the following block
Dim MyValue As Double
MyValue = 10 / 4  ' Returns 2.5.
MyValue = 10 / 3  ' Returns 3.333333.

\ Operator

Divides two numbers and returns an integer result.

Click here to copy the following block
Dim myValue As Integer
myValue = 11 \ 4  ' Returns 2.
myValue = 9 \ 3  ' Returns 3.
myValue = 100 \ 3  ' Returns 33.
MyValue = 67 \ -3  ' Returns -22.

Mod Operator

Divides two numbers and returns only the remainder.

Click here to copy the following block
Dim myResult As Double
myResult = 10 Mod' Returns 0.
myResult = 10 Mod' Returns 1.
myResult = 12 Mod 4.3  ' Returns 3.4.
myResult = 12.6 Mod' Returns 2.6.
myResult = 47.9 Mod 9.35  ' Returns 1.15.

+ Operator

Adds two numbers. Also used to concatenate two strings.

Click here to copy the following block
Dim myNumber As Integer
Dim myString As Integer

'// use + as arithmatic operator
myNumber = 2 + 2  ' Returns 4.
myNumber = 4257.04 + 98112  ' Returns 102369.04.

'// use + as concatation operator
myString = "AAA" + "222" 'Returns "AAA222"
myString = "Hello" + " " + "World" 'Returns "Hello World"

- Operator

Yields the difference between two numbers or indicates the negative value of a numeric expression.

Click here to copy the following block
Dim myResult As Double
myResult = 4 - 2  ' Returns 2.
myResult = 2 - 4  ' Returns -2.
myResult = 459.35 - 334.90  ' Returns 124.45.

Assignment Operators

= Operator

Used to assign a value to a variable or property

Click here to copy the following block
Dim myInt as Integer
Dim myString as String
Dim myButton as System.Windows.Forms.Button
Dim myObject as Object
myInt = 42
myString = "This is an example of a string literal"
myButton = New System.Windows.Forms.Button()
myObject = myInt
myObject = myString
myObject = myButton

^= Operator

Raises the value of a variable to the power of an expression and assigns the result back to the variable.

Click here to copy the following block
Dim var1 As Integer = 10
Dim var2 As Integer = 3
var1 ^= var2  ' The value of var1 is now 1000.

*= Operator

Multiplies the value of a variable by the value of an expression and assigns the result to the variable.

Click here to copy the following block
Dim var1 As Integer = 10
Dim var2 As Integer = 3
var1 *= var2  ' The value of var1 is now 30.

/= Operator

Click here to copy the following block
Dim var1 As Integer = 12
Dim var2 As Integer = 3
var1 /= var2  ' The value of var1 is now 4.

\= Operator

Divides the value of a variable by the value of an expression and assigns the integer result to the variable.

Click here to copy the following block
Dim var1 As Integer = 10
Dim var2 As Integer = 3
var1 \= var2  ' The value of var1 is now 3.

+= Operator

Adds the value of an expression to the value of a variable and assigns the result to the variable. Also concatenates a String expression to a String variable and assigns the result to the variable.

Click here to copy the following block
Dim var1 As Integer = 10
Dim var2 As Integer = 3
var1 += var2  ' The value of var1 is now 13.

' This example uses string variables.
Dim var1 As String = "10"
Dim var2 As String = "3"
var1 += var2  ' The value of var1 is now "103".

-= Operator

Subtracts the value of an expression from the value of a variable and assigns the result to the variable.

Click here to copy the following block
Dim var1 As Integer = 10
Dim var2 As Integer = 3
var1 -= var2  ' The value of var1 is now 7.

<<= Operator

Performs an arithmetic left shift on the value of a variable and assigns the result back to the variable.

Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the other end. In an arithmetic left shift, the bits shifted beyond the range of the result data type are discarded, and the bit positions vacated on the right are set to zero.

Click here to copy the following block
Dim Pattern As Short = 192 ' Bit pattern is 0000 0000 1100 0000.
Dim Result1, Result2, Result3, Result4, Result5 As Short

Result1= Pattern
Result2= Pattern
Result3= Pattern
Result4= Pattern
Result5= Pattern

Result1 <<= 0  ' Result is 192 (0000 0000 1100 0000). - same as Result1 = Result1 << 0
Result2 <<= 4  ' Result is 3072 (0000 1100 0000 0000). - same as Result2 = Result2 << 4
Result3 <<= 9  ' Result is -32768 (1000 0000 0000 0000). - same as Result3 = Result3 << 9
Result4 <<= 17 ' Result is 384 (0000 0001 1000 0000). - same as Result4 = Result4 << 17
Result5 <<= -1 ' Result is 0 (shifted 15 places to left). - same as Result5 = Result5 << -1

>>= Operator

Performs an arithmetic right shift on the value of a variable and assigns the result back to the variable.

Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the other end. In an arithmetic right shift, the bits shifted beyond the rightmost bit position are discarded, and the leftmost bit is propagated into the bit positions vacated at the left. This means that if variable has a negative value, the vacated positions are set to one. If variable is positive, or if its data type is Byte, the vacated positions are set to zero.

Click here to copy the following block
Dim Pattern As Short = 192 ' Bit pattern is 0000 0000 1100 0000.
Dim Result1, Result2, Result3, Result4, Result5 As Short

Result1= Pattern
Result2= Pattern
Result3= Pattern
Result4= Pattern
Result5= Pattern

Dim Pattern As Short = 2560  ' Bit pattern is 0000 1010 0000 0000.
Dim Result1, Result2, Result3, Result4, Result5 As Short
Result1 >>= 0  ' Result is 2560 (0000 1010 0000 0000).
Result2 >> 4  ' Result is 160 (0000 0000 1010 0000).
Result3 >> 10   ' Result is 2 (0000 0000 0000 0010).
Result4 = Result4 >> 18  ' Result is 640 (0000 0010 1000 0000).
Result5 = Result5 >> -1  ' Result is 0 (shifted 15 places to right).

&= Operator

Concatenates a String expression to a String variable and assigns the result to the variable.

Click here to copy the following block
Dim var1 As String = "Hello "
Dim var2 As String = "World!"
var1 &= var2  ' The value of var1 is now "Hello World!"

Comparison Operators

These operators that compare two expressions and return a Boolean value representing the result of the comparison.

Is Operator

Compares two object reference variables.

The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons. If object1 and object2 both refer to the same object, result is True; if they do not, result is False.

Click here to copy the following block
Dim myObject As New Object
Dim otherObject As New Object
Dim yourObject, thisObject, thatObject As Object
Dim myCheck As Boolean
yourObject = myObject
thisObject = myObject
thatObject = otherObject
myCheck = yourObject Is thisObject  ' Returns True.
myCheck = thatObject Is thisObject  ' Returns False.
myCheck = myObject Is thatObject ' Returns False, if myObject is not thatObject.

Like Operator

Compares two strings. If string matches pattern, result is True; if there is no match, result is False. If both string and pattern are an empty string. the result is True. Otherwise, if either string or pattern is an empty string, the result is False.

Characters in pattern Matches in string
? Any single character
* Zero or more characters
# Any single digit (0–9)
[charlist] Any single character in charlist
[!charlist] Any single character not in charlist


Click here to copy the following block
Dim myCheck As Boolean

myCheck = "F" Like "F"  ' Does "F" match "F"? Returns True.

myCheck = "F" Like "f"  ' Does "F" match "f"? Returns False.

myCheck = "F" Like "FFF" ' Does "F" match "FFF"? Returns False.

myCheck = "aBBBa" Like "a*a"  ' Does "aBBBa" have a "a" at the
  ' beginning, an "a" at the end, and any number of characters in
  ' between? Returns True.

myCheck = "F" Like "[A-Z]"  ' Does "F" occur in the set of
  ' characters from A to Z? Returns True.

myCheck = "F" Like "[!A-Z]" ' Does "F" NOT occur in the set of
  ' characters from A to Z? Returns False.

myCheck = "a2a" Like "a#a"   ' Does "a2a" begin and end with an
  ' "a" and have any single-digit number inbetween? Returns True.

myCheck = "aM5b" Like "a[L-P]#[!c-e]" ' Does "aM5b" fit the following
  ' pattern: Begins with "a", has and character from the set L through
  ' P, followed byb any single-digit number, and finally contains any
  ' character excluded from the character set c through e. Returns True.

myCheck = "BAT123khg" Like "B?T*" ' Does "BAT123khg" fit the
  ' following pattern: Begins with "B", followed by any single
  ' character, followed by a "T" and finally zero or more characters
  ' of any type. Returns True.

myCheck = "CAT123khg" Like "B?T*" ' Does "CAT123khg" fit the
  ' following pattern: Begins with "B", followed by any single
  ' character, followed by a "T" and finally zero or more characters
  ' of any type. Returns False.

Concatenation Operators

These operators join multiple strings into a single string.

+ and & Operators

Click here to copy the following block
Dim myString As String

myString= "Hello" & " World"
'Or
myString= "Hello" + " World"

Logical/Bitwise Operators

Logical Operators provide information on operators that compare Boolean expressions and return a Boolean result. Bitwise Operators perform low level bit operations.

And Operator

Performs a logical conjunction on two Boolean expressions, or bitwise conjunction on two numeric expressions.

Click here to copy the following block
Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Boolean

'//Logical operation
myCheck = A > B And B > C  ' Returns True.
myCheck = B > A And B > C  ' Returns False.

'//Bitwise operation
myCheck = (A And B)  ' Returns 8.
myCheck = (A And C)  ' Returns 2.
myCheck = (B And C)  ' Returns 0.

Not Operator

Performs logical negation on a Boolean expression, or bitwise negation on a numeric expression.

Click here to copy the following block
Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Boolean
'//logical operation
myCheck = Not(A > B)  ' Returns False.
myCheck = Not(B > A)  ' Returns True.

'//Bitwise operation (inverts all bits)
myCheck = (Not A)  ' Returns -11.
myCheck = (Not B)  ' Returns -9.
myCheck = (Not C)  ' Returns -7.

Or Operator

Used to perform a logical disjunction on two Boolean expressions, or bitwise disjunction on two numeric values..

If the operands consist of one Boolean expression and one numeric expression, the result Boolean expression will be converted to a numeric value (-1 for True, and 0 for False) and the bitwise operation will result.

Click here to copy the following block
Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Boolean

'//logical operation
myCheck = A > B Or B > C  ' Returns True.
myCheck = B > A Or B > C  ' Returns True.
myCheck = B > A Or C > B  ' Returns False.

'//Bitwise operation
myCheck = (A Or B)  ' Returns 10.
myCheck = (A Or C)  ' Returns 14.
myCheck = (B Or C)  ' Returns 14.

Xor Operator

Performs a logical exclusion operation on two Boolean expressions, or a bitwise exclusion on two numeric expressions.

For Boolean comparisons, if one and only one of the expressions evaluates to True, result is True. Otherwise, result is False. If either expression is stated as Nothing, that expression is evaluated as False.

If the operands consist of one Boolean expression and one numeric expression, the result Boolean expression will be converted to a numeric value (-1 for True, and 0 for False) and the bitwise operation will result.

Click here to copy the following block
Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Boolean

'//Logical operation
myCheck = A > B Xor B > C  ' Returns False.
myCheck = B > A Xor B > C  ' Returns True.
myCheck = B > A Xor C > B  ' Returns False.

'//Bitwise operation
myCheck = (A Xor B)  ' Returns 2.
myCheck = (A Xor C)  ' Returns 12.
myCheck = (B Xor C)  ' Returns 14.

AndAlso Operator

Performs short-circuiting logical conjunction on two expressions.

A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression. If the result of the first expression evaluated determines the final result of the operation, there is no need to evaluate the other expression, because it cannot change the final result. Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure calls.

Click here to copy the following block
Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6

Dim myCheck As Boolean

myCheck = A > B AndAlso B > C  ' True.
myCheck = B > A AndAlso B > C  ' False. Second expression not evaluated.
myCheck = A > B AndAlso C > B  ' False. Second expression evaluated.

A more common example for short-circuiting can be as follow.

Click here to copy the following block
Public Function FindValue(ByVal Arr() As Double, _
             ByVal SearchValue As Double) As Double
  Dim I As Integer = 0

  While I <= UBound(Arr) AndAlso Arr(I) <> SearchValue
  ' If I is greater than UBound(Arr), SearchValue is not checked.
   I += 1
  End While
  If I >= UBound(Arr) Then I = –1
  Return I
End Function

In the above example if you use And instead of AndAlso then it will throw an exception if Arr() is empty because Arr(I) <> SearchValue condition will be executed regardless of first condition.

OrElse Operator

Used to perform short-circuiting logical disjunction on two expressions.

Click here to copy the following block
Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Boolean
myCheck = A > B OrElse B > C  ' True. Second expression is not evaluated.
myCheck = B > A OrElse B > C  ' True. Second expression is evaluated.
myCheck = B > A OrElse C > B  ' False.

If MyFunction(5) = True OrElse MyOtherFunction(4) = True Then
' If MyFunction(5) is True, MyOtherFunction(4) is not called.
  ' Insert code to be executed.
End If

Bit Shift Operators

Ok so now not only C++ programmer can do bit shifting in one line but VB.net programmers have the same capability using bit shift operators. There are 2 new bit shift operators added in VB.net, Left shift operator(<<) and right shift operator(>>).

<< Operator

Performs an arithmetic left shift on a bit pattern.

Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the other end. In an arithmetic left shift, the bits shifted beyond the range of the result data type are discarded, and the bit positions vacated on the right are set to zero.

Click here to copy the following block
Dim Pattern As Short = 192  ' Bit pattern is 0000 0000 1100 0000.
Dim Result1, Result2, Result3, Result4, Result5 As Short
Result1 = Pattern << 0   ' Result is 192 (0000 0000 1100 0000).
Result2 = Pattern << 4   ' Result is 3072 (0000 1100 0000 0000).
Result3 = Pattern << 9  ' Result is -32768 (1000 0000 0000 0000).
Result4 = Pattern << 17   ' Result is 384 (0000 0001 1000 0000).
Result5 = Pattern << -1  ' Result is 0 (shifted 15 places to left).

>> Operator

Performs an arithmetic right shift on a bit pattern.

Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the other end. In an arithmetic right shift, the bits shifted beyond the rightmost bit position are discarded, and the leftmost (sign) bit is propagated into the bit positions vacated at the left. This means that if pattern has a negative value, the vacated positions are set to one; otherwise they are set to zero.

Click here to copy the following block
Dim Pattern As Short = 2560  ' Bit pattern is 0000 1010 0000 0000.
Dim Result1, Result2, Result3, Result4, Result5 As Short
Result1 = Pattern >> 0  ' Result is 2560 (0000 1010 0000 0000).
Result2 = Pattern >> 4  ' Result is 160 (0000 0000 1010 0000).
Result3 = Pattern >> 10   ' Result is 2 (0000 0000 0000 0010).
Result4 = Pattern >> 18  ' Result is 640 (0000 0010 1000 0000).
Result5 = Pattern >> -1  ' Result is 0 (shifted 15 places to right).


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.