Get Statement

Named Arguments

No

Syntax

Get [#]filenumber, [recnumber], varname


filenumber

Use: Required

Data Type: Integer

Any valid file number.


recnumber

Use: Optional

Data Type: Variant (Long)

Record or byte number.


varname

Use: Required

Data Type: Any

The variable into which the data is read.

Description

Copies data from a file on disk into a program variable.

Rules at a Glance

  • For files opened in random mode, recnumber refers to the record number in the file.

  • For files opened in binary mode, recnumber refers to the byte number within the file.

  • The number of bytes read by the Get statement is governed by the data type of varname.

  • The position of the first record or byte within a file is always 1.

  • Even if recnumber is omitted, you must use the delimiting commas (i.e., Get #1,,myVar).

  • When a record or number of bytes is read from a file using Get, the file pointer automatically moves to the record or byte following the one just read. You can therefore read all data from a random or binary file sequentially by omitting recnumber, as this snippet shows:

    Dim hFile as long
    hFile = FreeFile()
    Open sFileName For Random as #hFile
    Do While Not EOF(1)
       Get #1,,myVar
    Loop
    Close #hFile

  • Get is used most commonly to read data from files written with the Put statement.

Programming Tips and Gotchas

  • If you are using a Len clause with the Open statement and reading data from a random access file with the Get statement, you must be aware of the total number of bytes that the Get statement reads for each data field. This isn't always straightforward, since many data types use descriptors to inform programs reading them how many bytes the data takes up on the disk. For example, when reading a variant of a numeric subtype, Get first reads a two-byte descriptor, then the length of the data type. Your Len clause must therefore take account of these descriptors. You can obtain a complete list of all descriptors for each data type in the explanation of the Open statement.

  • It's usual practice to create a user-defined type to accept data from the file into your program. Your user-defined type must match both the data types and positions of the data in the file. You can then create an instance of your user-defined type to use as the variable, as the following example shows:

    Option Explicit
    
    Type MyType
        CustNo as Long
        CustName As String * 20
        CustPhone As String * 30
        CustZip As String * 10
    End Type
    
    Public CustDetails(10) As MyType
    
    Public Function ReadData() As Boolean
    
        Dim sFileName As String
        Dim i As Integer
        
        sFileName = "custData.dat"
        Open sFileName For Random As #1 Len = 64
        
        For i = 1 To 10
            Get #1, i, CustDetails(i)
        Next i
        Close #1
        
        MsgBox CustDetails(3).CustPhone
        
    End Function

  • With the increase in the power, flexibility, and ease of use of modern DBMSs, the use of external standalone data files has fallen dramatically, which means that statements such as Get and Open are fast becoming redundant. Furthermore, any application or user-specific configuration data is best kept in the registry, again reducing the need for external files.

  • If you use a Len clause in the Open statement, you should ensure that the value of Len matches the record length. It's possible to read records whose length is less than Len; however, you will then be unsure where the end of the record is, because the space between the length of Len and the end of the record will have been automatically padded with spurious characters.

See Also

Open Statement, Put Statement, Seek Function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset