unit Unit1;
我想做的是把一個過程myfun指向別一個過程subfun,可是我執行下面的代碼卻提示:
[Error] Unit1.pas(32): Left side cannot be assigned to
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure myfun;
public
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
procedure subfun;
begin
showmessage('my simple');
end;
begin
myfun:=subfun;
end;procedure TForm1.myfun;
begin
end;
end.
我想做的是把一個過程myfun指向別一個過程subfun,可是我執行下面的代碼卻提示:
[Error] Unit1.pas(32): Left side cannot be assigned to
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure myfun;
public
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
procedure subfun;
begin
showmessage('my simple');
end;
begin
myfun:=subfun;
end;procedure TForm1.myfun;
begin
end;
end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TMyFun = procedure;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FMyFun: TMyFun;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure subfun;
begin
showmessage('my simple');
end;procedure MyFun;
begin
ShowMessage('Original');
end;procedure TForm1.Button1Click(Sender: TObject);
begin
FMyFun := MyFun;
FMyFun;
FMyfun := subfun;
FMyFun;
end;
end.
procedure subfun;
begin
showmessage('my simple');
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Fmyfun:=myfun;
Fmyfun;
.....
.....
end;procedure TForm1.myfun;
begin
end;
end.
procedure myfunc;<--这是一个过程-->
private
myfunc: procedure;<--这才是过程变量,可以赋值的删除
procedure TForm1.myfun;
begin
end;还要注意,如果是对象的函数,则要定义成procedure of objectTProcTest = class
public
constructor Create;
public
procedure foo();
private
_prodTest: procedure of object;
end;constructorTProcTest.
var
p: procedure;
begin
p := foo;//错误
_prodTest := foo;//正确
end;