BUG: Order of Evaluation Change Between Native Code and P- Code
ID: Q191616
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
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.
MORE INFORMATION
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.
- Create the EXE by selecting Make Project1.EXE from the File menu.
- Run the EXE and note the results.
- Recompile the EXE by changing the project compile properties so that it
compiles to P-Code.
- 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.
Additional query words:
kbDSupport kbVBp600bug kbCompiler kbdss kbVBp kbVBp500bug
Keywords : kbGrpVB
Version :
Platform : WINDOWS
Issue type : kbbug