Microsoft KB Archive/191616

= BUG: Order of Evaluation Change Between Native Code and P- Code =

Article ID: 191616

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q191616



SYMPTOMS
A program appears to be incorrectly evaluating expressions, or evaluates differently between P-Code and Native Code with optimizations.



CAUSE
Visual Basic does not guarantee the order of evaluation in expressions when programs are compiled to native code with optimizations.



RESOLUTION
In a no-optimization case, the expression is evaluated left to right. In a optimized case, Visual Basic may change the order in which it evaluates expressions. A change in logic may be required to achieve the desired results.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.



Steps to Reproduce Behavior
 Create a new Standard EXE project in Microsoft Visual Basic. Form1 is created by default. Select Add Module from the Project menu to add a standard code module (Module1) to the project.  Copy and Paste the following code into Module1: Dim Sample As String

Public Sub Main

Dim Temp1 As String

While (get_String) And Not (Sample Like "*/*/*") MsgBox "In loop --> " & Sample & " " & Not _ (Sample Like "*/*/*") Wend MsgBox "after --> " & Sample End Sub

Function get_String As Boolean Sample = "09/19/97" get_String = True End Function

 Select Project Properties from the Project menu. Change the Startup Object to "Sub Main." Select the Compile tab and ensure the project is set to compile to Native Code.</li> Create the EXE by selecting Make Project1.EXE from the File menu.</li> Run the EXE and note the results.</li> Recompile the EXE by changing the project compile properties so that it compiles to P-Code.</li> Run the EXE again and note the different results. When compiled to P- Code, the expression 'While (get_String) And Not (Sample Like "*/*/*")' is evaluated as expected. However, when compiled to Native Code with optimizations, the (Sample Like "*/*/*") will be evaluated first.</li></ol>

Additional query words: kbDSupport kbVBp600bug kbCompiler kbdss kbVBp kbVBp500bug

Keywords: kbbug kbpending KB191616

-

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

© Microsoft Corporation. All rights reserved.