type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    F, G: function: Integer;
    function SomeFunction: Integer;
  public
    { Public declarations }
  end;
var
 Form1: TForm1;
implementation
{$R *.DFM}function TForm1.SomeFunction: Integer;
begin
  result:=23;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
  I: Integer;
begin
  F := SomeFunction;//-----------error
  I := F ;
  ShowMessage(IntToStr(I));
end;
end.
但是[Error] Unit1.pas(38): Incompatible types: regular procedure and method pointer

解决方案 »

  1.   

    谁告诉我如何实现用一个函数变量来执行ADOQuery的方法呢?
    eg: 
    var ExeProc: procedure
    m_Query :TAdoQuery;
    ....
      ExeProc:= m_Query.Open;  //error :类型不配套
      ...
      ExeProc:=m_Query.ExecSQL; //error :类型不配套这样我在线程对象里直接用:
    procedure TMyThread.Execute;
    begin
      ExeProc; //据当前函数变量调用实际引用的过程
    end;
      

  2.   

    呵呵,示例
    type
      TMyFunction=function(iId:integer):integer;
      TMyClass=class
      public 
        TstFunc:TMyFunction ;  
      end ;
    ......
    Function z(iId:integer):integer ;
    begin
      ...
      result:=... ;
    end ;
    ....
    begin
      .....
      TstFunc:=z ;
      .....
    end ;
      

  3.   

    unit Unit1;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;
    type
      TMyFunction=function(iId:integer):integer;  
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        TstFunc: TMyFunction ;
        Function z(iId:integer):integer ;
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;implementation{$R *.DFM}
    Function TForm1.z(iId:integer):integer ;
    begin
      result:=iId+10;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
       TstFunc:=z;   ////////////////////错误
    end;end.
    [Error] Unit1.pas(35): Incompatible types: regular procedure and method pointer
    [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
      

  4.   

    对象方法跟单纯的函数、过程是不同的类型。
    上面代码中,把TForm1定义中的“Function z”那行去掉,“Function TForm1.z(iId:integer):integer ;"中的“TForm1.”也去掉就行。
      

  5.   

    unit Unit1;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;
    type
      TMyFunction=function(iId:integer):integer;  
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        TstFunc: TMyFunction ;
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;implementation{$R *.DFM}
    function z(iId:integer):integer ;
    begin
      result:=iId+10;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
       TstFunc:=z;  
       showMessage(TstFunc(6)); 
    end;end.
      

  6.   

    skm (whathell?) :“那如果用函数指针调用对象的方法呢?”
    type
      TMyFunction=function(iId:integer):integer of object;