[Delphi]請問function type的使用方法 |
答題得分者是:william
|
cherry
一般會員 發表:16 回覆:26 積分:8 註冊:2002-06-28 發送簡訊給我 |
各位好:
請問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 發送簡訊給我 |
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 發送簡訊給我 |
不好意思喔, 我再描述清楚一點.. 類型一, 我會用
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 發送簡訊給我 |
不好意思喔, 我再描述清楚一點.. 類型一, 我會用
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 發送簡訊給我 |
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 發送簡訊給我 |
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 發送簡訊給我 |
引言: 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 發送簡訊給我 |
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 發送簡訊給我 |
|
cherry
一般會員 發表:16 回覆:26 積分:8 註冊:2002-06-28 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
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 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |