Sunday, March 15, 2020

The Anatomy of a Delphi Unit (for Beginners)

The Anatomy of a Delphi Unit (for Beginners) If you plan on being a good Delphi programmer than words like interface, implementation, and uses need to have special place in your programming knowledge. Delphi Projects When we create a Delphi application, we can start with a blank project, an existing project, or one of Delphis application or form templates. A project consists of all the files needed to create our target application.   The dialog box that pops up when we choose View-Project Manager lets us get access to the form and units in our project.   A project is made up of a single project file (.dpr) that lists all the forms and units in the project. We can look at and even edit the Project file (lets call it a  Project Unit) by choosing View - Project Source. Because Delphi maintains the project file, we should not normally need to modify it manually, and in general it is not recommended for inexperienced programmers to do so. Delphi Units As we know by now, forms are a visible part of most Delphi projects. Each form in a Delphi project also has an associated unit. The unit contains the source code for any event handlers attached to the events of the form or the components it contains. Since units store the code for your project, units are the basic of Delphi programming. Generally speaking, unit is a collection of constants, variables, data types, and procedures and functions that can be shared by several applications. Every time we create a new form (.dfm file), Delphi automatically creates its associated unit (.pas file) lets call it a  Form Unit. However, units dont have to be associated with forms. A  Code Unit contains code that is called from other units in the project. When you start building libraries of useful routines, you will probably store them in a code unit. To add a new code unit to Delphi application choose File-New ... Unit. Anatomy Whenever we create a unit (form or code unit) Delphi adds the following code sections automatically: unit header,  interface  section,  implementation  section. There are also two optional sections:  initialization  and  finalization. As you will see, units have to be in a  predefined  format so that the compiler can read them and compile the units code. The  unit header  starts with the reserved word  unit, followed by the units name. We need to use the units name when we refer to the unit in the uses clause of another unit. Interface Section This section contains the  uses  clause that lists the other units (code or form units) that will be used by the unit. In case of form units Delphi automatically adds the standard units such as Windows, Messages, etc. As you add new components to a form, Delphi adds the appropriate names to the uses list. However, Delphi does not add a uses clause to the interface section of code units- we have to do that manually. In the unit interface section, we can declare  global  constants, data types, variables, procedures and functions. Be aware that Delphi builds a form unit for you as you design a form. The form data type, the form variable that creates an instance of the form, and the event handlers are declared in the interface part.   Because there is no need to synchronize the code in code units with an associated form, Delphi does not maintain the code unit for you. Interface section  ends at the reserved word  implementation. Implementation Section The  implementation  section of a unit is the section that contains the actual code for the unit. The implementation can have additional declarations of its own, although these declarations arent accessible to any other application or unit. Any Delphi objects declared here would be available only to code within the unit (global to unit). An optional uses clause can appear in the implementation part and must immediately follow the implementation keyword. Initialization and Finalization Sections These two sections are optional; they are not automatically generated when you create a unit. If you want to  initialize  any data the unit uses, you can add an initialization code to the initialization section of the unit. When an application uses a unit, the code within the units initialization part is called before the any other application code runs.   If your unit needs to perform any cleanup when the application terminates, such as freeing any resources allocated in the initialization part; you can add a  finalization  section to your unit. The finalization section comes after the initialization section, but before the final end.