PRB: Print Function Evaluation Order Has Changed

ID: Q141879


The information in this article applies to:
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 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.

Additional query words:

Keywords : kbprg kbVBp400 IAPVBA VB4WIN vbwin
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type : kbprb


Last Reviewed: October 22, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.