Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 33712 - Last Review: August 16, 2005 - Revision: 2.1
"Subscript Out of Range" DIM SHARED Dynamic Array in SUBmodule
This article was previously published under Q33712
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).
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
APPLIES TO
- Microsoft QuickBASIC 4.0b
- Microsoft QuickBasic 4.5 for MS-DOS
- Microsoft BASIC Compiler 6.0
- Microsoft BASIC Compiler 6.0b
- Microsoft BASIC Professional Development System 7.0
Retired KB Content DisclaimerThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please