Microsoft KB Archive/35658

= &quot;Array Already Dimensioned&quot; if 2nd DIM for Static Array in IF =

Article ID: 35658

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0

-



This article was previously published under Q35658



SUMMARY
Static arrays are dimensioned at compile time, regardless of whether or not the DIM statement is in the program flow of control. For example, if the DIM for a static array occurs in an IF statement that would never be executed at run time, the array will still be dimensioned at compile time. If you attempt to dimension a given static array more than once in a source file, you will get an &quot;Array Already Dimensioned&quot; error on the second DIM statement at compile time, as in the following example: 10 x = 1 20 IF x = 2 THEN DIM array(14) 25 IF x = 1 THEN DIM array(25) ' &quot;Array Already Dimensioned&quot; compile-time 30 array(21) = 4 40 PRINT array(21) This behavior occurs in all versions of Microsoft QuickBasic for the IBM PC, in the Microsoft Basic Compiler Versions 5.35 and 5.36 for MS-DOS and Versions 6.00 and 6.00b for MS-DOS and MS OS/2, and in Microsoft Basic PDS Version 7.00 for MS-DOS and MS OS/2.



MORE INFORMATION
The QuickBasic compiler allocates static arrays as they are encountered in one top-to-bottom pass through the source file at compile time. In contrast, dynamic arrays are dimensioned only when the flow of control reaches a DIM statement at run time.

By default, arrays are static in the above compilers, unless you DIM them with a variable in the subscript. Adding the REM $DYNAMIC metacommand to the top of the program makes the arrays default to dynamic. Note that arrays are always dynamic in the Microsoft GW-Basic, IBM BasicA, and Compaq BasicA Interpreters.

The program below is another illustration. The compiled program will run without any error message, since the DIM for the static array is allocated at compile time. If you added a REM $DYNAMIC statement to make all arrays default to dynamic, the compiled program would give a &quot;Subscript out of Range&quot; error message at run time: 10 x = 1 20 IF x = 2 THEN DIM array(14) 30 array(11) = 4 40 PRINT array(11) The above program will give a &quot;Subscript out of Range&quot; error in the GW-Basic Version 3.20 interpreter, since the DIM for the dynamic array in the IF statement is not executed at run time, and the array in line 30 defaults to only ten elements.

Additional query words: QuickBas BasicCom

Keywords: KB35658

-

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

© Microsoft Corporation. All rights reserved.