Q59008: Bad Integer Output Using DEF FN, VAL, FOR-NEXT in BASIC 7.00
Article: Q59008 Product(s): See article Version(s): 7.00 | 7.00 Operating System(s): MS-DOS | OS/2 Keyword(s): ENDUSER | SR# S900217-5 buglist7.00 fixlist7.10 | mspl13_basic Last Modified: 1-AUG-1990 When run as an .EXE program (compiled with BC.EXE), the program below gives incorrect output in Microsoft BASIC Professional Development System (PDS) version 7.00 for MS-DOS and MS OS/2. The same program gives correct output when run in the QBX.EXE environment or compiled with the BC /D option. The program fails when it assigns an integer array (which is subscripted by a FOR...NEXT loop counter) to the VAL of a DEF FN string function operating on a temporary string that was assigned to an array element that is subscripted by the FOR...NEXT loop counter. Microsoft has confirmed this to be a problem in Microsoft BASIC Professional Development System (PDS) version 7.00. This problem was corrected in BASIC PDS version 7.10. This problem does not occur with an .EXE compiled in QuickBASIC version 4.50 or earlier. The program below outputs "2 2" instead of "1 2" from an .EXE program. (The problem occurs whether compiled as a stand-alone .EXE or as an .EXE requiring the BRT module). This problem is corrected by doing any one of the following: 1. Compiling with the BC /D option. 2. Removing the DEFINT A-Z line. 3. Replacing n (the ending value of the FOR...NEXT loop counter) with the constant 2 in the FOR...NEXT statement. 4. Eliminating the use of the temporary (placeholder) variable t$ by putting s$(i) in place of it in the VAL function. 5. Replacing d(i) with a nonarray (scalar) variable. Code Example ------------ DEFINT A-Z DIM s$(1 TO 2), d(1 TO 2) DEF FNa$ (x$) FNa$ = x$ END DEF CLS n = 2 s$(1) = "1" s$(2) = "2" PRINT FOR i = 1 TO n t$ = s$(i) d(i) = VAL(FNa$(t$)) ' One workaround is to use: d(i) = VAL(FNa$(s$(i))) PRINT d(i); NEXT END
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.
Copyright Microsoft Corporation 1986-2002.