Microsoft KB Archive/78956

= If DECLARE AS ANY, then Caution Since BC.EXE Won't Type-Check =

Article ID: 78956

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1
 * Microsoft QuickBasic 4.5 for MS-DOS, when used with:
 * Microsoft MS-DOS 6.22 Standard Edition

-



This article was previously published under Q78956



SUMMARY
The ANY keyword is an optional type that can be used in the &quot;AS {type}&quot; clause of the Basic DECLARE statement. While the AS ANY type will work for Basic routines, it is intended only for use when you DECLARE external procedures written in other (non-Basic) Microsoft languages.

If a parameter uses the AS ANY clause in a DECLARE statement for a Basic SUB or FUNCTION procedure, the program will operate normally in the QB.EXE or QBX.EXE environment in most cases. However, when the program is compiled with BC.EXE, BC.EXE cannot type-check parameters passed with the AS ANY clause. This may result in unexpected (zero, incorrect, or possibly hanging) parameter values if you pass parameter types that incorrectly match the types expected by the SUB or FUNCTION.

This information applies to Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS and MS OS/2, and to Microsoft QuickBasic version 4.5 for MS-DOS.



MORE INFORMATION
The following code demonstrates how the use of AS ANY in a DECLARE statement for a Basic routine can cause different behavior when running within the environment (QB.EXE or QBX.EXE) versus running as a compiled .EXE: ' The MyFunc$ function (below) converts a passed numeric value to a ' string, and prints the result: DECLARE FUNCTION MyFunc$ (A as ANY) DEFINT A-Z PRINT MyFunc$(10)      ' This passes 10 as an integer to                        ' FUNCTION MyFunc$ and prints the string result PRINT MyFunc$(10!)     ' Same as previous, only passes 10 ' as a single to MyFunc$ END

FUNCTION MyFunc$ (A) MyFunc$ = STR$(A) 'Converts the passed variable, A, into a string END FUNCTION When this code example is run in the QB.EXE or QBX.EXE environment, the following correct output results:

10

10

However, when the code is compiled and linked into an executable then run, the following incorrect output results:

10

0

You might expect that the compiler will produce the same results as the environment, but this is not the case. The BC.EXE compiler is a one-pass, top-down compiler, and has no way of determining in that pass the data type being passed to the function. This is because the ANY keyword overrides type checking for that argument. In this case, the DEFINT A-Z causes the function to take the first 2 bytes off the stack, (as a two-byte integer) instead of the 4 bytes of a single. The first 2 bytes, in this case, are zeros, thus the zero result when printed. The environment, however, is able to determine what parameter type the SUB/FUNCTION expects, and perform the correct coercion before passing the value to the SUB/FUNCTION.

The ANY keyword is meant only for external mixed-language routines that may require it. This is documented on page 92 in the &quot;Microsoft Basic 7.0: Language Reference&quot; manual for Basic PDS versions 7.0 and 7.1.

Additional query words: QuickBas BasicCom 7.00 7.10 4.50

Keywords: KB78956

-

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

© Microsoft Corporation. All rights reserved.