VBA: Difference between & and +

不羁岁月 提交于 2019-12-01 15:04:45

问题


What is the difference between:

string1 + string2

and

string1 & string2

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


回答1:


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.




回答2:


The two expressions are equivalent, but the operators are not. + can be used as an arithmetic operator as well as for string concatenation, & can only be used for the latter.



来源:https://stackoverflow.com/questions/3365197/vba-difference-between-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!