Reduce the units included in the uses

Post date: Mar 14, 2010 12:51:28 AM

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!

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:

1:

2:

3:

uses

uGetUserName, uSelectEmails, uGridTableData;

the unit code would look something like this

1:

2:

3:

4:

5:

6:

7:

8:

9:

10:

11:

12:

13:

14:

15:

16:

17:

18:

19:

20:

21:

22:

23:

24:

25:

26:

27:

28:

29:

30:

31:

32:

33:

34:

35:

36:

37:

38:

39:

40:

41:

42:

43:

44:

45:

46:

47:

48:

49:

50:

51:

52:

53:

54:

55:

56:

57:

58:

59:

60:

61:

62:

63:

64:

65:

66:

67:

68:

69:

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.

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:

1:

2:

3:

4:

implementation

uses uLibrary;

There is only 1 unit used in the main form.

The new unit code:

1:

2:

3:

4:

5:

6:

7:

8:

9:

10:

11:

12:

13:

14:

15:

16:

unit uLibrary;

interface

uses

uGetUserName,

uSelectEmails,

uGridTableData;

type

TfrmGetUserName = class(uGetUserName.TfrmGetUserName);

TfrmGetEmailTo = class(uSelectEmails.TfrmGetEmailTo);

TfrmShowGridData = class(uGridTableData.TfrmShowGridData);

implementation

end.

The rest of the main form still works, reducing the uses clause in the main form. :) by Geert_Gruwez