"Subscript Out of Range" DIM SHARED Dynamic Array in SUBmodule (33712)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0

This article was previously published under Q33712

SUMMARY

A "Subscript out of range" message displays at run time when a dynamic array is dimensioned using REM $DYNAMIC and DIM SHARED statements at the module level of a support (nonmain) module (composed of just subprogram or FUNCTION procedures). In contrast, if the DIM SHARED array is a statically dimensioned array instead of a dynamically dimensioned array, the program runs without the error message. This difference between dynamic and static arrays is not a problem with QuickBasic, but occurs because a DIM for a dynamic array is an executable statement (processed only if encountered in the flow of control at run time), whereas a DIM for a static array is a nonexecutable statement (always processed at compile time).

MORE INFORMATION

A DIM for a dynamic array is only executed at run time if the program's control flows through that DIM statement. Since no executable statements are ever executed at the module-level of a support (nonmain) module, the DIM statements for dynamic arrays at the module level of a support module are ignored. "Subscript out of Range" displays for the first time an undimensioned array is referenced in the subprogram.

In contrast, static arrays are allocated at compile time; and the DIM statement for a static array at the module-level of a support module is always recognized. A DIM statement for a static array is a compile-time, nonexecutable statement.

When compiling to an EXE file, be sure to produce debug code (that is, compile with the /D switch) to catch array problems.

To work around the compiler's ignoring of DIM SHARED for dynamic arrays at the module level of support modules, dimension the dynamic array in the main module (source file), and pass the array to the second module (separate source file) using the COMMON SHARED statement in both modules.

The following code fails with a "Subscript out of range" error when an element of the array is assigned to a value:

Module #1

   DECLARE SUB test()
   CALL test
				

Module #2

   REM $DYNAMIC            'will work if this is commented out
   DIM SHARED a(10,30,30)
   SUB test STATIC
   a(1,1,1) = 4           'Subscript out of range error
   PRINT a(1,1,1)
   END SUB
				
The following code successfully shares the dynamic array with the procedure in the support module:

Module #1

   DECLARE SUB test()
   REM $DYNAMIC
   COMMON SHARED a()
   DIM a(10,30,30)
   CALL test
				

Module #2

   COMMON SHARED A()
   SUB test STATIC
   a(1,1,1) = 4
   PRINT a(1,1,1)
   END SUB
				

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