Microsoft KB Archive/32271

= Passing Element of FIELDed Array to Subprogram UnFIELDs Array =

Article ID: 32271

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * 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

-



This article was previously published under Q32271



SUMMARY
The following information applies to QuickBasic Versions 4.00, 4.00b, and 4.50, to Microsoft Basic Compiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2, and to Microsoft Basic Professional Development System (PDS) Version 7.00 for MS-DOS and MS OS/2.

Passing an element of a FIELDed array to a subprogram unFIELDs the array, preventing the array from accessing the random file buffer.

QuickBasic Versions 3.00 and earlier do not unFIELD the array and allow you to access the random file buffer.



MORE INFORMATION
The program below works without modification in QuickBasic Versions 3.00 and earlier. The program needs to be modified (to pass the array elements by value or by a temporary variable) in QuickBasic Versions 4.00 and later because arrays are handled differently.

For QuickBasic Versions 4.00 and later, when an array element is passed to a subprogram or function, the array element is copied to a temporary variable and passed to the subprogram. Upon returning from the subprogram, the temporary variable value is assigned to the array element. This process is performed in case the array moves during the execution of the subprogram or function; it also &quot;unFIELDs&quot; the array.

Please note that in all versions of Basic, FIELDed variables are &quot;special&quot; and have limited use. For example, you are not allowed to say INPUT B$, where B$ is a FIELDed variable. You also cannot say B$=&quot;test&quot; and must instead say LSET B$=&quot;test&quot; or RSET B$=&quot;test&quot;.

This change in how the arrays are handled may affect any program that uses FIELDed arrays, including those applications that use Btrieve (a file-management system developed by SoftCraft, Inc.; Btrieve was later taken over by Novell, Inc.).

The easiest workaround is to pass a temporary variable instead of an array element in the CALL statement.

Another workaround for this problem is to put parentheses around the variable being passed in the CALL statement, thus causing it to be passed by value rather than by reference. An array passed by value is not unFIELDed. For example, the Code Example farther below could be changed as follows: CALL TEST((a$(i))) However, a disadvantage of passing by value is that if variables are large (such as a large string length), they could consume stack space quickly, possible leading to &quot;Out of Stack Space&quot; or &quot;Out of Memory&quot;.

The following code example demonstrates the unFIELDing of the array A$. DIM A$(2) OPEN &quot;TEST.DAT&quot; AS #1 FIELD #1, 10 AS A$(0), 10 AS A$(1), 10 AS A$(2) K = 65 FOR J = 1 to 2 FOR I = 0 TO 2 LSET A$(I)=STRING$(10,K) K = K + 1 NEXT I      PUT#1, j    NEXT J    CLOSE #1

OPEN &quot;TEST.DAT&quot; AS #1 FIELD #1, 10 AS A$(0), 10 AS A$(1), 10 AS A$(2) FOR J = 1 to 2 GET#1, J     FOR I = 0 to 2 CALL TEST(a$(i)) NEXT I     print NEXT J   END

SUB TEST(b$) STATIC PRINT b$   END SUB The output is as follows:   AAAAAAAA BBBBBBBB CCCCCCCC

AAAAAAAA BBBBBBBB CCCCCCCC As a workaround, the following modification can be made to the program to receive the desired output: GET#1, J  FOR I = 0 to 2 temp$=a$(i) CALL TEST(temp$) LSET a$(i)=temp$ NEXT I The correct output is as follows:   AAAAAAAA BBBBBBBB CCCCCCCC

DDDDDDDD EEEEEEEE FFFFFFFF

Additional query words: QuickBas BasicCom

Keywords: KB32271

-

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

© Microsoft Corporation. All rights reserved.