1. Home
  2. Computing & Technology
  3. Delphi Programming
Working with units
Page 1: What is a Delphi unit? The structure of a unit.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Page 2: Using Delphi's built-in unit files (RTL/VCL routines).
• Page 3: Creating and using your own code units.
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi Programming Course"
Discuss!
 Related Resources
• A Beginner's Guide to Delphi Programming.TOC

• RTL reference
• Creating functions and procedures

Welcome to the seventeenth chapter of the FREE online programming course:
A Beginner’s Guide to Delphi Programming.
While developing a (large) Delphi application, as program becomes more complex, its source code can become very hard to maintain. In this article you will learn about creating your own code modues - Delphi code files that contain logically associated functions and procedures. Along the process we'll briefly discuss using Delphi's built-in routines and how to make all the units of a Delphi application cooperate.

Here is an overview of the topics discussed in this chapter:

  • What is a Delphi unit? The structure of a unit.
  • Using Delphi's built-in unit files (RTL/VCL routines).
  • Creating and using your own code units.
  • The scope (visibility) of a Delphi identifier.
What is a Delphi unit?
In the fifth chapter of this course, we've opened a discussion on Delphi units. In general, a Delphi program is constructed from source-code modules called units. Each unit is stored in its own file (.pas) and compiled separately; compiled units are linked to create an application.
As stated, forms are visible building blocks of a Delphi application. Each form in a Delphi project 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.

Much of the work done by an application is performed by event-driven procedures; these provide the responses to the user's actions. Beside the source code that "drives" event handlers for the form and the component on it, you can write custom procedures or functions within a unit that's associated with a form. A custom procedure could be used, for example, to check whether a date entered in an edit box is valid.

However, units don't have to be associated with forms. If you want to reuse the routines that you write, it's better to create a separate unit to contain those routines. By creating stand-alone units that have no associated forms, you can easily make your procedures and functions available to other projects.

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. You can have any number of code units in your project so it is a good idea to split up your general-purpose procedures. The idea is to have one unit for date checking routines, another one for text-handling purposes and the third for dealing with databases.

Structure of a unit
I've already explained a structure of a Delphi unit (form unit and/or code unit) in the fifth chapter of this course, the next "illustration" summarizes what's already been stated:

unit MyUnit; //unit name
interface
{ In this section the routines (types, variables)
  are specified that are available for use in
  other units }
  uses
  { The list of the unit names whose
    routines our unit wants to use }
implementation
{ This section contains all the source code
  that defines all the types listed/defined
  in the interface section }
  uses
  { The optional list of the unit names with
    routines our unit wants to use }
initialization
{ This optional section contains all source code
  needed to be executed when the unit is
  referenced for the first time - to initialize
  any data the unit uses}
finalization
{ This optional section is required only if we have
  the initialization section. Here we write the code
  we need to be executed when the form is removed
  from memory. 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 the cleanup code into the finalization section}
end. //unit end

Now, that you know (in theory) what goes in a Delphi unit, it's time to see how to create Delphi programs that comprise a (larger) number of code units.

"A unit uses B unit" ?!
Stop for a minute, note the uses section/clause of the interface (or implementation) section... "The list of the unit names whose routines (types, variables ...) our unit wants to use". Uh, what a tricky sentence, what exactly does that mean? Who maintains this list? How do you know what unit your unit needs to use? How do you know what exact unit you need to add to the list if you want to use the "MatchesMask" Delphi function?

First, let's see how Delphi maintains the uses list automatically as you add components to a form...

Next page > How Delphi maintains a units source. Using Delphi's RTL routines. > Page 1, 2, 3

A Beginner's Guide to Delphi Programming: Next Chapter >>
>> Code navigation

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.