<

How to reduce your main form uses clause

Published on
12,802 Points
5,202 Views
6 Endorsements
Last Modified:
Approved
Geert G
"Did i really beat the cancer ?"
Nope, i didn't . Not yet anyway.
Now it's waiting, waiting, waiting ... until they say something.
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
Comment
Author:Geert G
5 Comments
LVL 22

Expert Comment

by:senad
Nice !
0
LVL 14

Expert Comment

by:systan
So, this could reduce application size?
0
LVL 39

Author Comment

by:Geert G
no, it does not reduce application size

it reduces the list of units used in the main unit
but they are used in other places

1 advantage if you have lots of common units in different projects
is when using a unit like uLibrary for each project
you can make it point to a different unit

let's say you have a unit uCustomer
this could be the same layout in 90% of your apps
but you may want to need something a tad different in the other 10%

simply creating a copy of the uCustomer unit,
making changes to it and then changing the uLibrary unit can solve this

this is also a possibilty if you don't have a versioning system
if you want to create a branch for some units, you could use this system too
0
LVL 8

Expert Comment

by:BdLm

does delphi has a similar feature as namespace in C++ ?
0
LVL 4

Expert Comment

by:rotem156
very nice tip, thank you.
0

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Join & Write a Comment

See the Basics of Office 365's Note Taking app, OneNote
Learn how to collaborate with office 365 Office Online

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month