VBA: Difference between & and +

l--*_*''' 8 vba concatenation operators

What is the difference between:

string1 + string2
Run Code Online (Sandbox Code Playgroud)

and

string1 & string2
Run Code Online (Sandbox Code Playgroud)

Are they equivalent? Why have two different symbols that do the same thing?

Mic*_*zek 7

The expressions are the same as long as the operands are strings; if not, + might add them instead depending on type conversions. & guarantees you won't get anything except a string concatenation, and will convert operands to strings if possible to do so.

There's an MSDN entry about Concatenation operations in Visual Basic that explains it:

The & Operator (Visual Basic) is defined only for String operands, and it always widens its operands to String, regardless of the setting of Option Strict. The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.

  • 如果其中一个值为null,则即使是字符串,表达式也不相同. (3认同)