ID: Q119115
The information in this article applies to:
With the introduction of Visual Basic for Applications as the language used in Microsoft Visual Basic version 4.0, the plus sign (+) operator has undergone a minor change in behavior.
The context rules used to determine whether to add or concatenate a mixture of variant and non-variant variables are now different.
Always use the ampersand (&) if you want to concatenate two strings.
The table listed below details the changes when adding or concatenating variant and non-variant data types. The main change is that if there is a numeric operand (rvalue) involved, Visual Basic will try to add the operands as numerics, coercing strings into numerics if possible. In previous versions of Visual Basic, the variant would be coerced into a string and concatenation performed.
In this example, the following variables are declared:
Dim n As Integer
Dim s As String
n = 4 ' numeric
s = "xyz" ' string
vn = 3 ' variant containing a number
vsn = "3" ' variant containing a string that can be coerced
' into a number
vss = "abc" ' variant containing a string that cannot be coerced
' into a number
Performing the following operations produces the following results:
Operation Old Result New Result Note*
n + "abc" compile error run-time error 1
n + "3" compile error 7 2
n + 3 7 7
s + "abc" xyzabc xyzabc
s + "3" xyz3 xyz3
s + 3 compile error run-time error 1
vn + "abc" 3abc run-time error 3
vn + "3" 33 6 3
vn + 3 6 6
vss + "abc" abcabc abcabc
vss + "3" abc3 abc3
vss + 3 run-time error run-time error
vsn + "abc" 3abc 3abc
vsn + "3" 33 33
vsn + 3 6 6
* Notes:
1. Run-time error instead of compile-time error.
2. Now adds instead of returning compile-time error.
3. Now adds (or tries to add) instead of concatenating. If you want to
concatenate, use the ampersand (&) operator.
Keywords : kbprg kbVBp400 PrgOther VB4WIN vbwin
Version : WINDOWS:2.0 3.0 4.0
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: October 1, 1997