How to reduce your main form uses clause

Geert GOracle dba
CERTIFIED EXPERT
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.
Published:
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
5,930 Views
Geert GOracle dba
CERTIFIED EXPERT
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.

Comments (5)

Top Expert 2010

Commented:
Nice !

Commented:
So, this could reduce application size?
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Author

Commented:
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

Commented:

does delphi has a similar feature as namespace in C++ ?
very nice tip, thank you.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.