PRB: Print Function Evaluation Order Has Changed

Last reviewed: September 30, 1997
Article ID: Q141879

The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SYMPTOMS

The order of evaluation of the arguments to the Print statement differs in Visual Basic 3.0 and Visual Basic 4.0.

CAUSE

All products that use Visual Basic for Applications, including Visual Basic 4.0, evaluate all function calls contained within a Print statement first, before any printing is done. Visual Basic 3.0 evaluates functions while executing the Print statement.

STATUS

This behavior is by design. The order of evaluation of multiple functions within an expression is undefined and may change from version to version. This is documented at the end of the online Help for the Function statement.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start Visual Basic.

  2. On the File menu, click New Project.

  3. Double-click the command button box on the toolbar (symbolized by a shaded oval). This will display a command button named Command1 on the form.

  4. Add the following code to the general declarations section of Form1:

          Function Test(N As Integer) As Integer
    
             N = N + 1
             Test = N
          End Function
    
    

  5. Add the following code to the command buttons click event:

          Sub Command1_Click()
             Dim M As Integer
             M = 1
             Form1.Print M, Test(M), M, Test(M)
          End Sub
    
    

  6. On the Run menu, click start (ALT, R, S) or press the F5 key.

The following chart illustrates the behavior of this program under Visual Basic 3.0 and Visual Basic 4.0:

                   Print M    Test(M)    M    Test(M)

Visual Basic 3.0   1          2          2     3
Visual Basic 4.0   3          2          3     3

Visual Basic 3.0 evaluates functions while processing the Print statement. Because Visual Basic 4.0 uses Visual Basic for Applications, all functions are evaluated first, before any printing is done.
Keywords          : IAPVBA VB4ALL VB4WIN vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS: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 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.