<

How to reduce your main form uses clause

Published on
13,037 Points
5,437 Views
6 Endorsements
Last Modified:
Approved
Geert G
These are my last weeks. If the doctors can reduce the pain I'll still get to september. Pity, but the Delphi ACE level is out of reach.
The uses clause is one of those things that just tends to grow and grow.
Most of the time this is in the main form, as it's from this form that all others are called.
If you have a big application (including many forms),
the uses clause in the interface and implementation section gets very big and messy.

Wouldn't it be nice to move all those child form unit names away from the main form ?

Well there is a way !
Just create a unit and alias the form types and procedures/functions and use only this unit in the main form uses clause

Let's consider a form for a user name (uGetUserName),
a form for selecting email adresses from a grid (uSelectEmails),
and a form for showing a grid with data from a table (uGridTableData)
all used from the main form.

so the implementation uses would have 3 units in it:
uses 
  uGetUserName, uSelectEmails, uGridTableData;

Open in new window


the unit code would look something like this
unit uMainForm; 
 
interface 
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;
  
type
  TfrmMain = class(TForm)
    labelUserName: TLabel;
    btnGetUserName: TBitBtn;
    editEmailTo: Edit;
    btnGetEmailTo: TBitBtn;
    btnShowGridData: TBitBtn;
    procedure btnGetUserNameClick(Sender: TObject);
    procedure btnGetEmailToClick(Sender: TObject);
    procedure btnShowGridDataClick(Sender: TObject); 
  end; 
 
var
  frmMain: TfrmMain; 
 
implementation 
 
uses uGetUserName, uSelectEmails, uGridTableData; 
 
{$R *.dfm } 
 
procedure TfrmMain.btnGetUserNameClick(Sender: TObject);
var 
  frm: TfrmGetUserName;
begin
  frm := TfrmGetUserName.Create(Self);
  try
    if frm.ShowModal = mrOk then 
      labelUserName.Caption := frm.editUserName.Text;
  finally
    FreeAndNil(frm);
  end;
end;    
  
procedure TfrmMain.btnGetEmailToClick(Sender: TObject);
var 
  frm: TfrmGetEmailTo;
begin
  frm := TfrmGetEmailTo.Create(Self);
  try
    if frm.ShowModal = mrOk then 
      editEmailTo.Text := frm.EmailTo;
  finally
    FreeAndNil(frm);
  end;
end;    
  
procedure TfrmMain.btnShowGridDataClick(Sender: TObject);   
var 
  frm: TfrmShowGridData;
begin
  frm := TfrmShowGridData.Create(Self);
  try
    frm.TableName := 'ARTICLES';
    frm.ShowModal;
  finally
    FreeAndNil(frm);
  end;
end;
 
end.

Open in new window


The uses section is small in this sample, just think if you were using 100+ forms like this.
Now I want to reduce the number of used units in my main form.
In this sample that's from 3 to 1.
In actual applications that could be from 100 or more to just the 1.
For this we need to create a intermediate unit which will then be replaced in the main form uses.
I called the new unit uLibrary

The changed implementation uses in the main form:
implementation 
  
uses uLibrary;

Open in new window


There is only 1 unit used in the main form.

the new unit code:
unit uLibrary; 
  
interface  
uses 
  uGetUserName,
  uSelectEmails,
  uGridTableData;
 
type 
  TfrmGetUserName = class(uGetUserName.TfrmGetUserName);
  TfrmGetEmailTo = class(uSelectEmails.TfrmGetEmailTo);
  TfrmShowGridData = class(uGridTableData.TfrmShowGridData);
  
implementation 
  
end.

Open in new window


The rest of the main form still works !

This is how to reduce your uses clause in the main form.
6
Author:Geert G
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free