You've seen all kinds of ways of setting up variables and strings, but what about doing something with that data? 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, which we'll use later in this chapter—see the topic "Using If…Else Statements" (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.