When you use the + operator, you may not be able to determine whether addition or string concatenation will occur. Use the & operator for concatenation to eliminate ambiguity and provide self-documenting code.
If at least one expression is not an Object, the following rules apply:
If | Then |
---|---|
Both expressions are numeric data types (Byte, Short, Integer, Long, Single, Double, or Decimal) | Add. |
Both expressions are String | Concatenate. |
One expression is a numeric data type and the other is any Object | Add. |
One expression is a String and the other is any Object. | Concatenate. |
One expression is a numeric data type and the other is a String | A Type mismatch error occurs. |
Either expression is Nothing. | A Variable Not Set error occurs. |
If both expressions are Object expressions, the following rules apply:
If | Then |
---|---|
Both Object expressions are numeric | Add. |
Both Object expressions are strings | Concatenate. |
One Object expression is numeric and the other is a string | A type mismatch error occurs. |
For simple arithmetic addition involving only expressions of numeric data types, the data type of result is usually the same as that of the most precise expression. The order of precision, from least to most precise, is Byte, Integer, Long, Single, Double, and Decimal. The following are exceptions to this order:
If | Then result is |
---|---|
A Single and a Long are added, | a Double. |
The data type of result is a Byte, Short, Integer or Long, object that overflows its legal range, | A Run-time error will occur. |
The data type of result is a Single or Doubleobject that overflows its legal range, | A Run-time error will occur. |
The data type of result is a Decimalobject that overflows its legal range, | A Run-time error will occur if the value is too large and Zero if the value is too small. |
If one or both expressions arestated as Nothing, a Variable Not Set
error occurs. If both expressions are Empty, result is an Integer. However, if only one expression is Empty, the other expression is returned unchanged as result.
Note The order of precision used by addition and subtraction is not the same as the order of precision used by multiplication.