The Delphi Language, Part 2 - The uses Clause and Circular Unit References
(Page 4 of 14 )
The uses clause is where you list the namespaces that you want to include in a particular program or unit. For example, if you have a program called FooProg that uses functions and types in two namespaces, UnitA and UnitB, the proper uses declaration is as follows:
program FooProg;
uses UnitA, UnitB;
Units can have two uses clauses: one in the interface section and one in the implementation section.
Here's code for a sample unit:
unit FooBar;
interface
uses BarFoo;
{ public declarations here }
implementation
uses BarFly;
{ private declarations here }
{definition of routines declared in interface section}
initialization
{ unit initialization here }
finalization
{ unit clean-up here }
end.
Note - The uses clause might have the fully qualified namespaces, or Delphi permits using just the innermost namespace name in the uses clause for backward compatibility (for example, Controls) using the namespace prefixes control of the Tools, Options, Delphi Options, Library dialog.
Circular Unit References Occasionally, you'll have a situation in which UnitA uses UnitB and UnitB uses UnitA. This is called a circular unit reference. The occurrence of a circular unit reference is often an indication of a design flaw in your application; you should avoid structuring your program with a circular reference. The optimal solution is often to move a piece of data that both UnitA and UnitB need to use out to a third unit. However, as with most things, sometimes you just can't avoid the circular unit reference. Note that circular references in both the interface or implementation section are illegal. Therefore, in such a case, move one of the uses clauses to the implementation part of your unit and leave the other one in the interface part. This usually solves the problem.
Packages and Assemblies Delphi packages enable you to place portions of your application into separate modules, which can be shared across multiple applications as .NET assemblies.
Packages and Assemblies are described in detail in Chapter 6, "Assemblies: Libraries and Packages."
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Object-Oriented Programming >>
More .NET Articles
More By Xavier Pacheco