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

[Delphi]請問function type的使用方法

答題得分者是:william
cherry
一般會員


發表:16
回覆:26
積分:8
註冊:2002-06-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-03-24 11:21:00 IP:203.74.xxx.xxx 未訂閱
各位好:   請問function type的使用方法.      Type     TBooleanMethod = function():Boolean of object;     TTestMethod = function():string of object;     procedure showtest (testMethod1:TBooleanMethod;testMethod2:TTestMethod);      以上的宣告我會用, 可是碰到需要帶值運算的不會耶..< > 想請問該怎麼做呢?? < > compile後會一直出現型態不符的現象.. T1BooleanMethod = function(const time_str : string):Boolean of object; TTestMethod = function():string of object; procedure showtest (testMethod1:TBooleanMethod;testMethod2:TTestMethod); 麻煩各位了..
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-03-24 11:53:30 IP:147.8.xxx.xxx 未訂閱
Typing error?    T1BooleanMethod = function(const time_str : string):Boolean of object; TTestMethod = function():string of object; procedure showtest (testMethod1:T1BooleanMethod;testMethod2:TTestMethod); What is the definition for testMethod1 and testMethod2?
cherry
一般會員


發表:16
回覆:26
積分:8
註冊:2002-06-28

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-03-24 13:45:54 IP:203.74.xxx.xxx 未訂閱
不好意思喔, 我再描述清楚一點.. 類型一, 我會用 type TBooleanMethod = function():Boolean of object; TTestMethod = function():string of object; function test2:string; function test3:Boolean; procedure showtest(TestMethod1:TBooleanMethod;TestMethod2:TTestMethod); procedure showtest(TestMethod1: TBooleanMethod; TestMethod2: TTestMethod); begin if Testmethod1=true then showmessage('true' Testmethod2) else showmessage('false' Testmethod2) end; // 實作時 showtest(test3,test2); 類型二, 其中的T1BooleanMethod, Compile 時會出現型態不符, 我不知道要怎麼寫... T1BooleanMethod = function(const time_str : string):Boolean of object; T1TestMethod = function:string of object; function bTest(date : string ): Boolean; function sTest:string; procedure show(TestMethod1:T1BooleanMethod;TestMethod2:T1TestMethod); procedure showtest(TestMethod1: TBooleanMethod; TestMethod2: TTestMethod); begin try TestMethod1;//這裡會出現型態不符 TestMethod2; finally MsgBox('error!'); end; end; // 實作時 showtest(btest,stest) 請再幫我看看, 謝謝.~
cherry
一般會員


發表:16
回覆:26
積分:8
註冊:2002-06-28

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-03-24 13:47:28 IP:203.74.xxx.xxx 未訂閱
不好意思喔, 我再描述清楚一點.. 類型一, 我會用 type TBooleanMethod = function():Boolean of object; TTestMethod = function():string of object; function test2:string; function test3:Boolean; procedure showtest(TestMethod1:TBooleanMethod;TestMethod2:TTestMethod); procedure showtest(TestMethod1: TBooleanMethod; TestMethod2: TTestMethod); begin if Testmethod1=true then showmessage('true' Testmethod2) else showmessage('false' Testmethod2) end; // 實作時 showtest(test3,test2); 類型二, 其中的T1BooleanMethod, Compile 時會出現型態不符, 我不知道要怎麼寫... T1BooleanMethod = function(const time_str : string):Boolean of object; T1TestMethod = function:string of object; function bTest(date : string ): Boolean; function sTest:string; procedure show(TestMethod1:T1BooleanMethod;TestMethod2:T1TestMethod); procedure showtest(TestMethod1: TBooleanMethod; TestMethod2: TTestMethod); begin try TestMethod1;//這裡會出現型態不符 TestMethod2; finally MsgBox('error!'); end; end; // 實作時 showtest(btest,stest) 請再幫我看看, 謝謝.~
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-03-24 14:00:48 IP:147.8.xxx.xxx 未訂閱
You need to specify the arguments...    
引言: 不好意思喔, 我再描述清楚一點.. 類型一, 我會用 type TBooleanMethod = function():Boolean of object; TTestMethod = function():string of object; function test2:string; function test3:Boolean; procedure showtest(TestMethod1:TBooleanMethod;TestMethod2:TTestMethod); procedure showtest(TestMethod1: TBooleanMethod; TestMethod2: TTestMethod); begin if Testmethod1=true then showmessage('true' Testmethod2) else showmessage('false' Testmethod2) end; // 實作時 showtest(test3,test2); 類型二, 其中的T1BooleanMethod, Compile 時會出現型態不符, 我不知道要怎麼寫... T1BooleanMethod = function(const time_str : string):Boolean of object; T1TestMethod = function:string of object; function bTest(date : string ): Boolean; function sTest:string; procedure show(TestMethod1:T1BooleanMethod;TestMethod2:T1TestMethod); procedure showtest(TestMethod1: TBooleanMethod; TestMethod2: TTestMethod); begin try TestMethod1('your time_str here');//這裡會出現型態不符 TestMethod2; finally MsgBox('error!'); end; end; // 實作時 showtest(btest,stest) 請再幫我看看, 謝謝.~
cherry
一般會員


發表:16
回覆:26
積分:8
註冊:2002-06-28

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-03-24 14:24:41 IP:203.74.xxx.xxx 未訂閱
Hi william: Thanks your reply, but I still have some question.. procedure TMyDateTime.ConfirmTime(SetTimeMethod: TSetTimeMethod; GetTimeMethod: TGetTimeMethod); var str : String; begin try SetTimeMethod(str);//這樣宣告對嗎? 上層的FUNCTION會自動將值傳入嗎 GetTimeMethod; finally MsgBox('Error!'); end; end; //Call this procedure ConfirmTime(SetTimeStr0('20030431'),GetTimeStr3()); 但是這裡會出現 Incompatible types... 要怎麼做啊?
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-03-24 14:31:08 IP:147.8.xxx.xxx 未訂閱
引言: Hi william: Thanks your reply, but I still have some question..
procedure TMyDateTime.ConfirmTime(SetTimeMethod: TSetTimeMethod; GetTimeMethod: TGetTimeMethod);
var
  str : String;
begin
  try
     SetTimeMethod(str);//這樣宣告對嗎? 上層的FUNCTION會自動將值傳入嗎
Syntax correct but str is an undefined local variable         GetTimeMethod;
  finally
    MsgBox('Error!');
  end;    end;
//Call this procedure ConfirmTime(SetTimeStr0('20030431'),GetTimeStr3()); 但是這裡會出現 Incompatible types... 要怎麼做啊? procedure TMyDateTime.ConfirmTime(const time_str: string; SetTimeMethod: TSetTimeMethod; GetTimeMethod: TGetTimeMethod); {....} SetTimeMethod(time_str); {....} ConfirmTime('20030431',SetTimeStr0,GetTimeStr3());
cherry
一般會員


發表:16
回覆:26
積分:8
註冊:2002-06-28

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-03-24 14:46:14 IP:203.74.xxx.xxx 未訂閱
Hi William: 再請問一下,我參考你的寫法實作後, 發現仍然會出現: Incompatible types:TSetMethod and String; 請問是那裡錯了? procedure TMyDateTime.ConfirmTime(const time_str: string; SetTimeMethod: TSetTimeMethod; GetTimeMethod: TGetTimeMethod); begin try SetTimeMethod(time_str); GetTimeMethod; finally MsgBox('The date is not valid date!'); end; end; ConfirmTime(('20030431'),GetTimeStr3()); Thanks Cherry
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-03-24 14:57:13 IP:147.8.xxx.xxx 未訂閱
ConfirmTime('20030431',SetTimeStr0,GetTimeStr3);
cherry
一般會員


發表:16
回覆:26
積分:8
註冊:2002-06-28

發送簡訊給我
#10 引用回覆 回覆 發表時間:2003-03-24 15:32:56 IP:203.74.xxx.xxx 未訂閱
Hi William: Sorry my careless... ConfirmTime('20030431', SetTimeStr0, GetTimeStr3); but error message Incompatible types 'regular procedure and method pointer' Thanks Cherry
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#11 引用回覆 回覆 發表時間:2003-03-24 15:55:31 IP:147.8.xxx.xxx 未訂閱
Are SetTimeStr0, GetTimeStr3 methods of object? If not, I think you defined the wrong types:    T1BooleanMethod = function(const time_str : string):Boolean; TTestMethod = function():string;    
引言: Hi William: Sorry my careless... ConfirmTime('20030431', SetTimeStr0, GetTimeStr3); but error message Incompatible types 'regular procedure and method pointer' Thanks Cherry
系統時間:2024-05-08 10:14:27
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!