|
在别的单元中 uses 自定义函数单元; 直接使用自定义函数; 例: ———————————————————————— unit Unit2; interface implementation function max(x,y:integer):integer; var tmp:integer; begin tmp:=x; if y> x then tmp:=y; Result:=tmp; end; end. ———————————————————— unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses Unit2; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption:=IntToStr(max(12,16)); end; end. 但总是: [Error] Unit1.pas(30): Undeclared identifier: 'max' |
|
|
|
unit Unit2;
interface function max(x,y:integer):integer; implementation function max(x,y:integer):integer; var tmp:integer; begin tmp:=x; if y> x then tmp:=y; Result:=tmp; end; end. |
|
|
将单元2加到interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,单元2; |
|
|
在单元中alt+F11选中要引用的单元
单元名称.函数的名称 就可以了 |
|
|
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,unit2; type TForm1 = class(TForm) Label1: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption:=IntToStr(form2.max(12,16)); end; end. unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm2 = class(TForm) private { Private declarations } public function max(x,y:integer):integer; end; var Form2: TForm2; implementation {$R *.dfm} function tform2.max(x,y:integer):integer; var tmp:integer; begin tmp:=x; if y> x then tmp:=y; Result:=tmp; end; end. end. |
|
|
调试通过,给分啊!
|
|