After you declare a name for some data, the next thing is usually to do some operation with the data. Microsoft classifies all Visual Basic operators as:
- Arithmetic Operators
- Comparison Operators
- Concatenation Operators
Arithmetic operations include the familiar "+", "*", and "/" but also including interesting variations such as the modulus (Mod) and integer division "\" and a nice collection of operations that get down to single bits (1's and 0's), the bitwise And, Or, and bit shift (">>" and "<<"). One key concept to remember is that Visual Basic will apply these operators in a default sequence if you don't add parenthesis to group things. The statement ...
i = 4 + 6 * 7
... results in a value of 46 in the variable i. But ...
i = (4 + 6) * 7
... results in a value of 70 in i.
The purpose of a comparison operator is to produce a Boolean true or false value that can be used somewhere else in your program, usually in something like an If block or a program loop.
There are six comparison operators:
- = (Equality)
- <> (Inequality)
- < (Less than)
- > (Greater than)
- <= (Less than or equal to)
- >= (Greater than or equal to)
It's worth knowing that the Boolean value that is a result of a comparison operation is just True or False. It's not a simple number like it used to be in VB6. Microsoft has this to say about the Boolean data type:
When numeric data types are converted to Boolean values, 0 becomes False and all other values become True. When Boolean values are converted to numeric types, False becomes 0 and True becomes -1.
Some old VB6 programs use the values 0 and -1 instead of True and False. Never do that in VB.NET.
Microsoft puts the logical operators, And, Or, AndAlso, OrElse, Xor and the Not operator in their own category because they only operate on Boolean variables. If you ever get confused about how these work, a Practical Guide to Logical Expressions is available on this site here.
There are just two concatenation operators, "+" and "&", which both do almost the same thing: join two strings into one. "+" is also an arithmetic operator so if you only want to concatenate strings, use "&". Here's an example:
Dim myString As String = "This" & " is " & " a " & " String."
On the next page, we use Boolean variables to write conditional statements. They're really the fundamental trick that gives computers their power.