線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1549
推到 Plurk!
推到 Facebook!

如何動態呼叫function or procedure -- (2)

尚未結案
ystzou
一般會員


發表:2
回覆:2
積分:0
註冊:2003-10-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-11-27 19:28:30 IP:218.175.xxx.xxx 未訂閱
想再問,若要呼叫的 function or procedure 不是在 class 內 該如何下手 ?? 以及 TMethod 的詳細用法?? 我查了 Delphi Help , TMethod 是一個 Record 內有 Code 與 Data 二個 Pointer =============================== var p : pointer; m : TMyFunc; begin p := MethodAddress('aa'); TMethod(m).Code := p; TMethod(m).Data := self; m(); end; =============================== 其中 TMethod(m) <== 為何要如此使用呢 ??,且 Data 應設為何 ?? 先謝謝回答了,此問題需接續 (如何動態呼叫function or procedure) 文章 不便之處,非常抱歉..
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-11-27 22:54:22 IP:218.32.xxx.xxx 未訂閱
引言: 想再問,若要呼叫的 function or procedure 不是在 class 內 該如何下手 ??
沒有 TObject 的 "MethodAddress", 我只想到用 TStringList 紀錄 Procedure List , 參考看看:
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, TypInfo;    type
  TMyPorcdure = procedure (Sender: TObject);      TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  Procedure P1(Sender: TObject);
  Procedure P2(Sender: TObject);    var
  Form1: TForm1;
  ProcList: TStringList;    implementation    {$R *.dfm}
Procedure P1(Sender: TObject);
begin
  showmessage('In P1 Procedure');
end;    Procedure P2(Sender: TObject);
begin
  showmessage('In P2 Procedure');
end;    procedure TForm1.Button1Click(Sender: TObject);
var m : TMyPorcdure;
    s : string;
    id : Integer;
begin
  s := 'P1';
  id := ProcList.IndexOf(s);
  if id<>-1 then begin
    m := TMyPorcdure(ProcList.Objects[id]);
    m(Sender);
  end
  else raise Exception.CreateFmt('Procedure %s not exists',[s]);      s := 'P2';
  id := ProcList.IndexOf(s);
  if id<>-1 then begin
    m := TMyPorcdure(ProcList.Objects[id]);
    m(Sender);
  end
  else raise Exception.CreateFmt('Procedure %s not exists',[s]);
end;    initialization
  ProcList := TStringList.Create;
  ProcList.AddObject('P1',TObject(@P1));
  ProcList.AddObject('P2',TObject(@P2));
finalization
  ProcList.Clear;
  ProcList.Free;
end.
引言: 其中 TMethod(m) <== 為何要如此使用呢 ??,且 Data 應設為何 ??
TMethod(m) 只是 Cast M as TMethod Record. A method pointer is really a pair of pointers; the first stores the address of a method, and the second stores a reference to the object the method belongs to.
ystzou
一般會員


發表:2
回覆:2
積分:0
註冊:2003-10-21

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-11-28 19:32:32 IP:218.175.xxx.xxx 未訂閱
謝謝 MicKey 您的回答...
chiehmin
高階會員


發表:13
回覆:134
積分:134
註冊:2002-05-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-12-16 18:30:07 IP:61.221.xxx.xxx 未訂閱
那如果P1和P2放在private裡那該怎麼作呢?    http://delphi.ktop.com.tw/topic.php?TOPIC_ID=40505 用這篇裡的方法都無法找到P1和P2....    用此篇的方法似乎也不行???< >< >
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-12-16 21:41:00 IP:218.32.xxx.xxx 未訂閱
節錄一段 Help : A private member is invisible outside of the unit or program where its class is declared. In other words, a private method cannot be called from another module, and a private field or property cannot be read or written to from another module. By placing related class declarations in the same module, you can give the classes access to one another's private members without making those members more widely accessible. 人家的 Private, 怎可以給外人看呢...哈哈. 要 Run-Time 動態找到 Private Method, 大概不行. 發表人 - Mickey 於 2003/12/16 21:43:57
系統時間:2024-03-28 15:56:53
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!