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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With