条款17使用swap
The uses clause is one of those things that just tends to grow and grow.
Uses子句是那些只会不断增长的事物之一。
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.
接口和实现部分中的uses子句变得非常大而混乱。
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
只需创建一个单元并为表单类型和过程/函数添加别名,并在主表单use子句中仅使用此单元
Let's consider a form for a user name (uGetUserName),
让我们考虑一下用户名(uGetUserName)的表单,
a form for selecting email adresses from a grid (uSelectEmails),
用于从网格中选择电子邮件地址的表格(uSelectEmails),
and a form for showing a grid with data from a table (uGridTableData)
以及用于显示带有表格数据的表格的表格(uGridTableData)
all used from the main form.
所有从主要形式使用。
so the implementation uses would have 3 units in it:
因此实现用途将包含3个单元:
uses
uGetUserName, uSelectEmails, uGridTableData;
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.
The uses section is small in this sample, just think if you were using 100+ forms like this.
在此示例中,uses部分很小,只要考虑一下您是否正在使用100多种这样的表单。
Now I want to reduce the number of used units in my main form.
现在,我要减少主要表单中使用的单位数。
In this sample that's from 3 to 1.
在此示例中,从3到1。
In actual applications that could be from 100 or more to just the 1.
在实际应用中,可能从100甚至更多到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;
There is only 1 unit used in the main form.
主表单中仅使用1个单位。
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.
The rest of the main form still works !
其他主要形式仍然有效!
This is how to reduce your uses clause in the main form.
这是减少主窗体中的using子句的方法。
翻译自: https://www.experts-exchange.com/articles/1008/How-to-reduce-your-main-form-uses-clause.html
条款17使用swap