在Delphi中避免使用循环引用

时间:2011-07-05 08:09:09

标签: delphi

我有一个名为RCLowPass的单​​位,它使用Unit2。它使用对象TForm2。

Unit2也使用RCLowPass。

Delphi抱怨循环引用,因为一个模块需要另一个模块。

以下是相关单位的相关样本。

RCLowPass:

unit RCLowPass;

interface

uses
  ComplexMath, ExtraMath, Unit2;

procedure SelectRCLowPassFilter;

implementation

{ This is why I need Unit2. }
procedure SelectRCLowPassFilter;
begin
  // Setup form for RC Low Pass
  TForm2.Prop1Name.Caption := 'R1';
  TForm2.Prop1Name.Visible := true;
  TForm2.Prop2Name.Caption := 'C1';
  TForm2.Prop2Name.Visible := true;
  TForm2.Prop3Name.Visible := false;
  TForm2.Prop4Name.Visible := false;
  TForm2.Prop5Name.Visible := false;
  TForm2.Prop6Name.Visible := false;
  TForm2.Prop7Name.Visible := false;
  TForm2.Prop8Name.Visible := false;
  TForm2.Prop1Value.Visible := true;
  TForm2.Prop2Value.Visible := true;
  TForm2.Prop3Value.Visible := false;
  TForm2.Prop4Value.Visible := false;
  TForm2.Prop5Value.Visible := false;
  TForm2.Prop6Value.Visible := false;
  TForm2.Prop7Value.Visible := false;
  TForm2.Prop8Value.Visible := false;
end;

end.

UNIT2:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, RCLowPass, ExtCtrls;

var
  Form2: TForm2;

implementation

uses RCLowPass;

{$R *.dfm}

{ This is why I need RCLowPass. }
procedure TForm2.Button1Click(Sender: TObject);
var
  v : real;
begin
  ShowMessage('Output of RC lowpass with R=1k, C=100n');
  v := RCLowPassZAtFreq(1000, 100e-9, 10);
  ShowMessage('@10Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 10);
  ShowMessage('@20Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 20);
  ShowMessage('@50Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 50);
  ShowMessage('@100Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 100);
  ShowMessage('@200Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 200);
  ShowMessage('@500Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 500);
  ShowMessage('@1000Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 1000);
  ShowMessage('@2000Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 2000);
  ShowMessage('@5000Hz: ' + FormatFloat('#.########', v));
  v := RCLowPassZAtFreq(1000, 100e-9, 5000);
  ShowMessage('@10000Hz: ' + FormatFloat('#.########', v));
end;

procedure SelectFilter(filter : integer);
begin
  if filter = 0 then
    SelectRCLowPassFilter();
end;

end.

如何修复循环引用?

1 个答案:

答案 0 :(得分:3)

unit RCLowPass;
interface

uses
  ComplexMath, ExtraMath;

procedure SelectRCLowPassFilter;

implementation
uses
  Unit2; // <<-- HERE

{ This is why I need Unit2. }
procedure SelectRCLowPassFilter;
begin
  // Setup form for RC Low Pass
  TForm2.Prop1Name.Caption := 'R1';
  TForm2.Prop1Name.Visible := true;
  ...