Microsoft KB Archive/119115

= PRB: Plus Sign (+) Operator Behaves Differently in VB 4.0 =

Article ID: 119115

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition

-



This article was previously published under Q119115



SYMPTOMS
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.



CAUSE
The context rules used to determine whether to add or concatenate a mixture of variant and non-variant variables are now different.



RESOLUTION
Always use the ampersand (&) if you want to concatenate two strings.



MORE INFORMATION
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.

Example Scenario
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: kbprogramming kbprb KB119115

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.