Microsoft KB Archive/31426

= &quot;Duplicate Definition&quot; on STATIC Array in Second CALL to SUB =

Article ID: 31426

Article Last Modified on 11/21/2006



This article was previously published under Q31426



SUMMARY
In the following sources, the example of using the STATIC statement does not clearly explain what will happen with a second call to the SUB:


 * 1) Page 80 of the &quot;Microsoft QuickBasic 4.0: Programming in Basic: Selected Topics&quot; manual for Versions 4.00 and 4.00b
 * 2) Page 70 of the &quot;Microsoft QuickBasic: Programming in Basic&quot; manual for Version 4.50

If SubProg2 is called more than once, the DIM statement gives you a &quot;Duplicate Definition&quot; error message. This is because in a recursive procedure, arrays are always dimensioned dynamically (that is, at run time), and making the array retain its values between calls (with the STATIC statement) means that the array was already dimensioned at the second call.

The example should be changed as follows:


 * 1) Declare an additional STATIC variable as a flag.
 * 2) Put the DIM in an IF...END IF block that executes only upon first call to the routine, and not on subsequent calls, as determined by the flag variable.

This correction also applies to the following:


 * 1) Page 80 of &quot;Microsoft Basic Compiler Version 6.00 for MS OS/2 and MS-DOS: Programming in Basic: Selected Topics&quot; for the Basic compiler Versions 6.00 and 6.00b
 * 2) Page 66 of &quot;Microsoft Basic 7.0: Programmer's Guide&quot; for Microsoft Basic PDS Version 7.00 for MS-DOS and MS OS/2



MORE INFORMATION
Removing the array from the STATIC statement also eliminates the &quot;Duplicate Definition.&quot;

The following code demonstrates how to avoid the &quot;Duplicate Definition&quot; error: DECLARE SUB dummy PRINT &quot;call the subroutine&quot; CALL dummy PRINT &quot;call the routine again&quot; CALL dummy

SUB dummy STATIC a, FirstPassFlag  ' STATIC retains values between CALLs ' FirstPassFlag assures that array gets DIMensioned only once. IF FirstPassFlag = 0 THEN DIM a(1) FirstPassFlag = 1 END IF print &quot;continuing on&quot; END SUB

Additional query words: QuickBas BasicCom

Keywords: KB31426

-

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

© Microsoft Corporation. All rights reserved.