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



The information in this article applies to:

  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 7.1
  • Microsoft QuickBASIC 4.5, when used with:
    • the operating system: MS-DOS

This article was previously published under Q78956

SUMMARY

The ANY keyword is an optional type that can be used in the "AS {type}" 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 "Microsoft Basic 7.0: Language Reference" manual for Basic PDS versions 7.0 and 7.1.

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB78956