Creating Units


Our practice so far has been to create functions and procedures in the main Delphi project file. Although this is allowed and is useful in really small projects, you should try to create functions and procedures in separate source code files (units). A unit is a plain text file that contains Delphi source code. Units allow you to reuse functions and procedures in multiple projects.

Delphi allows you to create a new unit even when there are no open projects, but it doesn't allow you to compile the unit. Delphi only compiles projects. So, before we can create a new unit, we have to create a new console application project and save it to disk.

To create a new unit, you can select File ® New ® Unit - Delphi for Win32, or you can double-click the Unit item in the Delphi Projects/Delphi Files category of the Tool Palette. Delphi automatically adds the new unit to the project (see Figure 5-2) and shows the unit source code in the Code Editor:

image from book
Figure 5-2: New unit

unit Unit1; interface implementation end. 

The first line is the unit heading that specifies the unit name. The unit name specified after the reserved word unit must match the file name. So, if the unit name is Unit1, the file name must be image from book Unit1.pas.

Every unit is separated into two distinct sections: the interface and implementation sections. The interface section is the public part of the unit that contains declarations that can be used from other units and project files. It begins with the reserved word interface and ends with the reserved word implementation.

The implementation section of the unit is the private part of the unit that contains declarations and statements that can only be used by other statements in the same unit. This part of the unit starts with the reserved word implementation and usually ends at the end of the entire unit.

Using Units

When you add a new unit to the project, Delphi automatically adds the unit reference to the project uses list:

uses   SysUtils,   Unit1 in 'Unit1.pas';

Delphi uses the UnitName in FileName syntax in the uses list when it doesn't know the exact location of the unit or when the unit is not in the search path. The in syntax is also used to differentiate between units that belong to the project and those that don't. In this case, Delphi uses the in syntax because the file hasn't been saved to disk, and therefore it can't determine if the file is in the search path or not. If you save the unit outside of the search path or outside of the project directory, Delphi adds the absolute file path to the uses list:

uses   SysUtils,   Unit1 in 'C:\Unit1.pas'; 

If you save the unit to the project directory, Delphi only adds the unit file name, but still uses the UnitName in FileName syntax. In this case, the in syntax tells Delphi that the unit is local to the project. The in syntax only changes Delphi IDE functionality. For instance, if the unit is referenced with the in syntax, you can double-click the unit in the Project Manager window and Delphi will open it. If you remove the in syntax, Delphi will treat the unit as an outside unit and you will lose a few IDE options.

If you create units that contain utility functions and procedures that can successfully be reused in multiple projects, you should create a separate directory for all your units and add that directory to the search path.

You can modify the search path in the Project Options or the Default Project Options dialog box. If you're currently working on a project, you can only display the Project Options dialog box, which is accessed by selecting Project ® Options. If the IDE is empty, you can display the Default Project Options dialog box by selecting Project ® Default Options ® Delphi for Win32.

To see the current search path, select the Directories/Conditionals item on the left side of the dialog box (see Figure 5-3). When the search path is empty, you can only use standard Delphi units from the InstallDir\lib directory.

image from book
Figure 5-3: Default Project Options dialog box

To add your own units to the search path, click the (…) button to the right of the Search path text box. The Directories dialog box that appears helps you add directories to the search path.

image from book
Figure 5-4: Directories dialog box

Once you've added all necessary directories to the search path, you may want to make these options the default for all new projects. If you are modifying options in the Default Project Options dialog box, these settings are already set as the default for new projects. If you are modifying options in the Project Options dialog box and want to set them as the default, you have to select the Default option in the lower-left corner of the dialog box.

Adding Code to the Unit

Units can contain code that is private to the unit or available for public use. If you define a constant or a variable in the interface section, the variable or constant is public and can be used outside of the unit.

Listing 5-6A: Public constants and variables in a unit

image from book
unit Unit1; interface const   PUBLIC_CONST = 'Hello'; var   PublicVar: string; implementation end.
image from book

Listing 5-6B: Using public constants and variables

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils,   Unit1 in 'Unit1.pas'; begin   PublicVar := PUBLIC_CONST;   WriteLn(PublicVar);   ReadLn; end.
image from book

Unlike constant and variable declarations, you cannot add a procedure to the interface part of the unit. Procedures and functions can only be written in the implementation section of the unit.

unit Unit1; interface const   PUBLIC_CONST = 'Hello'; var   PublicVar: string; implementation procedure About; begin   WriteLn(PUBLIC_CONST, ' from Unit1.'); end; end.

The About procedure can currently be called only by other procedures in the same unit. If you want to use the About procedure in other units, you have to declare it in the interface section of the unit. To declare a function or a procedure, you only have to copy the procedure or function header to the interface section of the unit.

unit Unit1; interface const   PUBLIC_CONST = 'Hello'; var   PublicVar: string; procedure About; implementation procedure About; begin   WriteLn(PUBLIC_CONST, ' from Unit1.'); end; end. 

Delphi units can have a third section, the initialization section. This section gives you the ability to execute procedures or statements at application startup. If a unit has an initialization section, the implementation section ends at the beginning of the initialization section. The initialization section can only be defined after the implementation section.

Listing 5-7: Unit initialization

image from book
unit Unit1; interface procedure About; implementation procedure About; begin   WriteLn('Made with Delphi.'); end; initialization   About; end.
image from book



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net