Bad Random GET Record Order with TYPE's Element as Next Record (45055)



The information in this article applies to:

  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5

This article was previously published under Q45055

SUMMARY

The following issue can arise when you use user-defined TYPEs, and each record in your random-access file contains a field that points to the next record number.

It has been reported that when running a compiled Basic program, it is possible to get inconsistent results when the record-number argument of a random-access GET is based upon an element of a user-defined-TYPE record that is input in the same GET statement.

The following is an example:
   GET #1, ARec.NextRec, ARec
				
ARec.NextRec is an element of the user-defined record ARec, and is also the pointer to the next record number in the file (which is known as a linked-list file structure).

The behavior of such a GET statement has been reported as inconsistent from a compiled .EXE program, and records may be read in the wrong order. The same program works correctly running from within the QB.EXE environment.

Microsoft does not advise using an element that you are reading data into as the pointer to the record being read in the same GET statement. Instead, assign the element containing the next record to be read to a temporary variable, and use that temporary variable in the next GET statement.

This information applies to QuickBasic Versions 4.00, 4.00b, and 4.50, and to the Basic Compiler Versions 6.00 and 6.00b.

MORE INFORMATION

The following code fragment helps explain the problem (but is not sufficiently complete to reproduce the error):
  TYPE atype
      NextRec as integer
      CurrentData as single
   END TYPE
   DIM ARec as atype
   ' Please avoid doing GETs such as the following, which use an
   ' element of the input record as the next record number to input
   ' in the same GET statement:
   GET #1, ARec.NextRec, ARec
				
As a workaround, use a temporary variable to specify the next record to GET:
   TYPE atype
      NextRec as integer
      CurrentData as single
   END TYPE
   DIM ARec as atype
   temp%=ARec.NextRec
   GET #1, temp%, ARec
				

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB45055