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

Last reviewed: September 29, 1997
Article ID: Q119115
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0, and 3.0

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          : PrgOther VB4ALL VB4WIN vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS:2.0 3.0 4.0
Platform          : WINDOWS
Issue type        : kbprb


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.