<

Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x

How to reduce your main form uses clause

Published on
12,529 Points
4,929 Views
6 Endorsements
Last Modified:
Geert Gruwez
6 months until my next "Did i really beat the cancer ?" check
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 22

Expert Comment

by:senad
Nice !
0
 
LVL 14

Expert Comment

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

Author Comment

by:Geert Gruwez
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Join & Write a Comment

This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month