delphi 自定义函数声明
如果需要让别的单元可以调用,可以新建一个公共单元并在interface部分,var声明如下。
unit Unit_common;
interface
uses
SysUtils;//如有必要还可添加其它单元名。
var
function mytest(i : integer): string;
implementation
function mytest(i: integer): string;
var
//如有函数的私有变量可在此声明
begin
i := i + 1;
result := inttostr(i);
end;
调用此函数时可在调用的单元中的user(在interface或implementation部分效果一样)部分增加此单元的单元名,例如:
unit login;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, jpeg, ExtCtrls, Grids, Calendar, ComCtrls,
Mask, DBCtrls, Unit_common, main, datamodel;
type
Tfrm_login = class(TForm)
Image1: TImage;
Label1: TLabel;
....................
procedure DateTimePicker1KeyPress(Sender: TObject; var Key: Char);
procedure btn_okClick(Sender: TObject);
procedure btn_cancelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frm_login: Tfrm_login;
implementation
{$R *.dfm}
procedure Tfrm_login.btn_okClick(Sender: TObject);
var
a:string;
begin
.......
a:=mytest(5);
.......
end;