+ Operator

Description

Used to sum two numbers.

Syntax

result = expression1+expression2

The + operator syntax has these parts:

Part

Description

result

Any numeric variable.

expression1

Any expression.

expression2

Any expression.


Remarks

Although you can also use the + operator to concatenate two character strings, you should use the & operator for concatenation to eliminate ambiguity and provide self-documenting code.

When you use the + operator, you may not be able to determine whether addition or string concatenation will occur. If at least one expression is not a Variant, the following rules apply:

If

Then

Both expressions are numeric data types (Boolean, Integer, Long, Single, Double, Date or Currency)

Add.

Both expressions are String

Concatenate.

One expression is a numeric data type and the other is any Variant (except a Null)

Add.

One expression is a String and the other is any Variant (except a Null)

Concatenate.

One expression is an Empty Variant

Return the remaining expression unchanged as result.

One expression is a numeric data type and the other is a String

A Type mismatch error occurs.

Either expression is a Null

result is a Null.


If both expressions are Variant expressions, the underlying type of the expressions determines the behavior of the + operator in the following way:

If

Then

Both Variant expressions are numeric

Add.

Both Variant expressions are strings

Concatenate.

One Variant expression is numeric and the other is a string

Add.


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 Integer, Long, Single, Double, and Currency. The following are exceptions to this order:

If one or both expressions are Null expressions, result is a Null. If both expressions are Empty, result is an Integer. However, if only one expression is Empty, the other expression is returned unchanged as result.

See Also

Operator Precedence.

Example

This example uses the + operator to sum numbers. The + operator can also be used to concatenate strings but to eliminate ambiguity, you should use the & operator instead.


MyNumber = 2 + 2    ' Returns 4.
MyNumber = 4257.04 + 98112    ' Returns 102369.04.
Var1 = "34" : Var2 = 6    ' Initialize variables.
MyNumber = Var1 + Var2    ' Returns 40.
Var1 = "34" : Var2 = "6"    ' Initialize variables.
MyNumber = Var1 + Var2    ' Returns "346" (string concatenation).