Variables and literal data values are involved in processing operations in order to create new information from the original values. The two most common ways to manipulate constants and variables are through arithmetic and string operations. Visual Basic supplies arithmetic and string operators to contruct these processing statements, or expressions.
Arithmetic Operators
Much of information processing involves applying arithmetic to numeric data types. Values are added, subtracted, multiplied, and divided to generate results, either final results or intermediate values that are involved in further arithmetic operations. For these purposes Visual Basic supplies a set of seven arithmetic operators shown in the following table.
| Operator | Use |
|---|---|
| + | Addition. Adds the values appearing on the two sides of the operator. |
| - | Subtraction. Subtracts the value on the right of the operator from the value on the left of the operator. |
| * | Multiplication. Multiplies the values appearing on the two sides of the operator. |
| / | Division. Divides the value on the left of the operator (dividend) by the value on the right of the operator (divisor). |
| \ | Integer Division. Divides the value on the left of the operator by the value on the right of the operator, giving an Integer result. |
| Mod | Modulo Division. Returns the remainder of the division of the value on the left of the operator by the value on the right of the operator. |
| ^ | Exponentiation. Raises the value on the left of the operator to the power on the right of the operator. |
Multiple operators can be combined with multiple constants and variables to create complex expressions.
Addition
Two numeric values are added by linking them with the addition operator. The result of the operation can be assigned to a variable of a compatible numeric type.
Subtraction
Two numeric values are subtracted by linking them with the subtraction operator. The result of the operation can be assigned to a variable of a compatible numeric type.
Multiplication
Two numeric values are multiplied by linking them with the multiplication operator. The result of the operation can be assigned to a variable of a compatible numeric type.
Division
Two numeric values are divided by linking them with the division operator. The value on the left is the dividend; the value on the right is the divisor. The result of the operation can be assigned to a variable of a compatible numeric type.
Integer Division
Two integer values are divided by linking them with the Integer Division operator. The value on the left is the dividend; the value on the right is the divisor. The result is the largest integer that is less than the absolute value of the quotient of the two operands. The result of the operation can be assigned to a variable of a compatible numeric type.
Modulo Division
Two numeric values are divided by linking them with the Modulo Division operator. The value on the left is the dividend; the value on the right is the divisor. The result is the remainder of the division of the two values. The result of the operation can be assigned to a variable of a compatible numeric type.
Exponentiation
A numeric value is raised to a power by linking the value with a power value through the Exponentiation operator. The result of the operation can be assigned to a variable of a compatible numeric type.
Operator Precedence
When an arithmetic expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. Multiplication and division take precedence, and are performed first, over addition and subtraction. For example, in the expression
x + y / zvariable y is divided by variable z with the result then added to variable x. The expression is evaluated as x + (y / z) because the / operator has higher precedence than the + operator. If you are not careful in combining operators you can end up with unexpected results. In the previous example, you would generate the wrong answer if your intent was to first add x + y and then divided by z.
Complex arithmetic expressions created with these operators can be, and should be, enclosed within parentheses to explicitly control the order in which the operations are carried out. Explicitly code x + (y / z) or (x + y) / z depending on your intent.
Arithmetic Assignment Operators
Normally, the results of arithmetic operations are assigned to a variable using the standard assignment operator "=". There are, in addition, special assignment operators that perform an arithmetic operation and a variable assignment at the same time. These operators are listed in the following table.
| Operator | Use |
|---|---|
| += | Adds the value appearing on the right of the operator to the variable on the left of the operator. The statement X += Y is equivalent to X = X + Y. |
| -= | Subtracts the value appearing on the right of the operator from the variable on the left of the operator. The statement X -= Y is equivalent to X = X - Y. |
| *= | Multiplies the value appearing on the right of the operator times the variable on the left of the operator and replaces the variable with the new value. The statement X *= Y is equivalent to X = X * Y. |
| /= | Divides the variable on the left of the operator by the value on the right of the operator and replaces the variable with the new value. The statement X /= Y is equivalent to X = X / Y. |
| \= | Divides the Integer variable on the left of the operator by the Integer value on the right of the operator and replaces the variable with the new value. The statement X \= Y is equivalent to X = X \ Y. |
| ^= | Raises the variable on the left of the operator to the power on the right of the operator and replaces the variable with the new value. The statement X ^= Y is equivalent to X = X ^ Y. |
A quick example of the += operator will give you an idea of how these operators work. This operator is often used to accumulate totals. Assume you have declared a variable named Total that is used to sum up a set arithmetic computations. In the following code, variable A is added to Total, variable B is added to Total, and variable C is added to Total. Following this program, variable Total contains the value 30.
Dim A As Integer = 5 Dim B As Integer = 10 Dim C As Integer = 15 Dim Total As Integer = 0 Total += A Total += B Total += C
Other popular uses for the += and -= operators are to increment a counter (Counter += 1) or to decrement a counter (Counter -= 1). In the first case, 1 is added to the Counter each time the statement is executed; in the second case, 1 is subtracted from the Counter.
String Operators
Visual Basic supplies the concatenation operator (&) to put together individual data strings to create a longer string of those combinations. The Like operator looks for matching characters in a string.
| Operator | Use |
|---|---|
| & or + | Concatenation. Appends the string value appearing on the right of the operator to the string value on the left of the operator. |
| Like | Determines whether a string matches a given pattern. |
String Concatenation
Concatenation of strings can take place with string variables and/or string constants. In the following example, three String variables are declared. A string expression concatenates the first two variables along with a literal blank space to create a complete sentence that is assigned to the third variable.
Note that the plus sign (+) also can be used as a concatenation operator. This symbol is used for concatenation, rather than addition, because of the context in which it is applied. If two string are linked with the + symbol, then concatenation takes place because two strings cannot be added. If the + symbol links a string and a number, then concatenation also takes place. Since a string cannot be added, the number is automatically converted into a string and the two values are concatenated.
String Assignment Operator
Similar to arithmetic assignment operators, there are special string assignment operators that perform a concatenation operation and a variable assignment at the same time. These operators are listed in the following table.
| Operator | Use |
|---|---|
| &= or += | Concatenates the value appearing on the right of the operator to the variable on the left of the operator. The statement X &= Y is equivalent to X = X &= Y. |
Assume the following code:
Dim A As String = "This " Dim B As String = "is " Dim C As String = "a string." Dim Sentence As String Sentence &= A Sentence &= B Sentence &= C
At the end of this program, variable Sentence contains the full string "This is a string." Each statement appends a string value to the continually lengthening Sentence variable.
Matching Strings
The Like operator determines whether a string matches a given pattern. The operand on the left is the string being matched, the operand on the right is the pattern to match against. The result of the expression
| string value Like string value |
is the Boolean value True or False depending on the success of the match.
The default behavior of the Like operator is to evaluate the expression as an exact match. Therefore, the statements,
Dim String_A As String = "This is a string." Dim String_B As String = "This is a string." Dim Matched As Boolean Matched = String_A Like String_B
result in the Boolean value True being assigned to variable Matched. The match is on a character-by-character, left-to-right basis; plus, the match is case sensitive. The string "THIS IS A STRING." does not match string "This is a string.".
Wildcard characters can be used in the pattern for partial matches. These characters, along with special characters lists, are described in the following table.
| Pattern | Meaning |
|---|---|
| ? | Matches any single character at the position. For example, the expression "ABCDE" Like "AB?DE" returns True because it matches any character in the third position of the string as long as the remaining characters match exactly. |
| * | Matches zero or more characters at the position. For example, the expression "ABCDE" Like "*CDE" returns True because it matches any characters at the beginning of the string followed by "CDE" at the end of the string. Likewise, "ABCDE" Like "AB*" returns True since the two strings begin with "AB" irrespective of any following characters. The expression "ABCDE" Like "*D*" returns True because the string contains "D" preceded and followed by any number of characters. |
| # | Matches any single digit (0 - 9) at the position. For example, the expressions "12345" Like "12#45" and "12845" Like "12#45" return True because they match any decimal digit in the third position of the string as long as the remaining characters match exactly. |
| [char list] | Matches any single character in [char list]. For example, the expression "C" Like "[ABCDE]" returns True because the character "C" is found in the list of characters "ABCDE". |
| [!char list] | Matches any single character not in [char list]. For example, the expression "M" Like "[!ABCDE]" returns True because the character "M" is not found in the list of characters "ABCDE". |
When matching against a [char list] the list can be specified as an ascending range of characters. To match against the upper-case alphabet, for instance, the character list is "[A-Z]"; to match against the decimal digits, the character list is "[0-9]".
The [char list] can be matched only against a single character. Thus, you cannot match a word to see if all its characters are included in the character list. You would need to compare each character individually with the character list by using a program loop (see later).