Download Delphi BX Quick start manual
Transcript
Unit references and the uses clause So, for example, if you have defined data structures that need to be initialized, you can do this in the initialization section. For units in the interface uses list, the initialization sections of units used by a client are executed in the order in which the units appear in the client’s uses clause. The finalization section The finalization section is optional and can appear only in units that have an initialization section. The finalization section begins with the reserved word finalization and continues until the end of the unit. It contains statements that are executed when the main program terminates (unless the Halt procedure is used to terminate the program). Use the finalization section to free resources that are allocated in the initialization section. Finalization sections are executed in the opposite order from initializations. For example, if your application initializes units A, B, and C, in that order, it will finalize them in the order C, B, and A. Once a unit’s initialization code starts to execute, the corresponding finalization section is guaranteed to execute when the application shuts down. The finalization section must therefore be able to handle incompletely initialized data, since, if a runtime error occurs, the initialization code might not execute completely. Unit references and the uses clause A uses clause lists units used by the program, library, or unit in which the clause appears. (For information about libraries, see Chapter 9, “Libraries and packages”). A uses clause can occur in • the project file for a program or library, • the interface section of a unit, and • the implementation section of a unit. Most project files contain a uses clause, as do the interface sections of most units. The implementation section of a unit can contain its own uses clause as well. The System unit and the SysInit unit are used automatically by every application and cannot be listed explicitly in the uses clause. (System implements routines for file I/O, string handling, floating point operations, dynamic memory allocation, and so forth.) Other standard library units, such as SysUtils, must be included in the uses clause. In most cases, all necessary units are placed in the uses clause when your project generates and maintains a source file. In unit declarations and uses clauses (on Linux particularly), unit names must match the file names in case. In other contexts (such as qualified identifiers), unit names are case insensitive. To avoid problems with unit references, refer to the unit source file explicitly: uses MyUnit in "myunit.pas"; Programs and units 3-5