Adds two numbers. Also used to concatenate two strings.
expression1 + expression2
The value of the result will be the sum of expression1 and expression2 if expression1 and expression2 are numeric or the result of the concatenation of expression1 and expression2 if expression1 and expression2 are strings.
Byte, Short, Integer, Long, Single, Double, Decimal, String.
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 neither expression is an Object, the following rules apply:
If | Then |
---|---|
Both expressions are the same numeric data types (Byte, Short, Integer, Long, Single, Double, or Decimal) | Add. |
Both expressions are strings | Concatenate. |
One expression is a numeric data type and the other is a string | If Option Strict is On, a compile error is generated; if Option Strict is Off, implicitly convert String to Double and add. If String cannot be converted to a numeric value, an InvalidCastException is thrown. |
One expression is a numeric data type, and the other is Nothing | If Option Strict is On a compile error is generated; if Option Strict is Off,
add, with Nothing valued at zero. |
One expression is a string, and the other is Nothing | If Option Strict is On a compile error is generated; if Option Strict is Off, concatenation, with Nothing = "". |
If one expression is an Object expression, the following rules apply.
If | Then |
---|---|
One expression is a numeric Object expression and the other is a numeric value type | If Option Strict is On, a compiler error is generated; if Option Strict is Off, add. |
One expression is a numeric Object expression and the other is a String type | If Option Strict is On, a compiler error is generated; if Option Strict is Off, implicitly convert String to Double and add. If String cannot be converted to a numeric value, an InvalidCastException is thrown. |
One expression is a string Object expression and the other is a String type | If Option Strict is On, a compiler error is generated; if Option Strict is Off, implicitly convert Object to String, and then concatenate. |
One expression is a string Object expression and the other is a numeric value type | If Option Strict is On, a compiler error is generated; if Option Strict is Off, implicitly convert Object to Double and add. If Object cannot be converted to a numeric value, an InvalidCastException is thrown. |
If both expressions are Object expressions, the following rules apply (Option Strict Off only):
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 | Implicitly cast the numeric Object to Double and add. If Object cannot be converted to a numeric value, an InvalidCastException is thrown. |
If one or both expressions are stated as Nothing or have a value of DBNull, they are treated as a string with a value of "".
This example uses the + operator to add numbers. You can also use the + operator to concatenate strings. However, to eliminate ambiguity, you should use the & operator instead. If the components of an expression created with the + operator are numeric, the arithmetic result is assigned. If the components are exclusively strings, the strings are concatenated. The expression cannot contain components of mixed types. The arithmetic result returns the sum of the two operands. The concatenation result returns a string representing the concatenation of the two operands.
Dim myNumber As Integer Dim var1 As String Dim var2 As Integer myNumber = 2+
2 ' Returns 4. myNumber = 4257.04+
98112 ' Returns 102369.04. Option Strict On ' Initialize mixed variables. var1 = "34" var2 = 6 myNumber = var1+
var2 ' Generates a compile-time error. Option Strict Off Var1 = "34" Var2 = 6 myNumber = var1 + var2 ' Returns 40 (addition) after the string in var1 is ' converted to a numeric value. Use of Option Strict Off ' for these operations is not recommended.
& Operator | Concatenation Operators | Arithmetic Operators | Operators Listed by Functionality | Operator Precedence in Visual Basic |