Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between VBScript's + and & operator?

On every site that talks about VBScript, the '&' operator is listed as the string concatenation operator. However, in some code that I have recently inherited, I see the '+' operator being used and I am not seeing any errors as a result of this. Is this an accepted alternative?

like image 338
Jeremy Cron Avatar asked Jul 30 '09 19:07

Jeremy Cron


People also ask

What is the difference between VB and VBS?

VB is a full-fledged programming language which can be used to create compiled applications, while VBScript is a sub-set of VB and is a scripting language that can be used to run a set of commands, similar to an old-school DOS batch file.

Are VBA and VBS the same?

Let's take a look at a few of the larger differences between VBScript and VBA: VBScript is an untyped language. Unlike Visual Basic and Visual Basic for Applications, in which the developer can define the data type of a variable in advance, all variables in VBScript are variants.

What is the difference between Visual Basic and VBA?

VBA stands for Visual Basic For Applications and its a Visual Basic implementation intended to be used in the Office Suite. The difference between them is that VBA is embedded inside Office documents (its an Office feature). VB is the ide/language for developing applications.


2 Answers

The & operator does string concatenation, that is, forces operands to be converted to strings (like calling CStr on them first). +, in its turn, forces addition if one of the expressions is numeric. For example:

1 & 2 

gives you 12, whereas

1 + 2 "1" + 2 1 + "2" 

give you 3.

So, it is recommended to use & for string concatenation since it eliminates ambiguity.

like image 65
Helen Avatar answered Oct 04 '22 10:10

Helen


The + operator is overloaded, whereas the & operator is not. The & operator only does string concatenation. In some circles the & operator is used as a best practice because it is unambiguous, and therefore cannot have any unintended effects as a result of the overloading.

like image 37
Robert Harvey Avatar answered Oct 04 '22 11:10

Robert Harvey