怎樣在delphi中自定義Procedure |
尚未結案
|
6925251
一般會員 ![]() ![]() 發表:7 回覆:14 積分:4 註冊:2003-07-09 發送簡訊給我 |
|
a123473119
一般會員 ![]() ![]() 發表:19 回覆:46 積分:18 註冊:2002-08-08 發送簡訊給我 |
|
Chance36
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:31 回覆:1033 積分:792 註冊:2002-12-31 發送簡訊給我 |
|
6925251
一般會員 ![]() ![]() 發表:7 回覆:14 積分:4 註冊:2003-07-09 發送簡訊給我 |
例如定義一個public procedure,max(a,b),傳回a,b中最大值,代碼如下,好像編譯不通過,請您舉個例子!
unit Unit1; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type
TForm1 = class(TForm)
private
{ Private declarations }
public procedure max(a,b);
var
a,b:inteter;
begin
if a>b then
max(a,b):=a
else
max(a,b):=b;
end;
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.DFM} end. Delphi初學者,急盼指教!
------
吳江鵬 |
Chance36
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:31 回覆:1033 積分:792 註冊:2002-12-31 發送簡訊給我 |
6925251 你好
引言:_______________________________________ 深藍的魚,祝您好運..........連.連例如定義一個public procedure,max(a,b),傳回a,b中最大值,代碼如下,好像編譯不通過,請您舉個例子! unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } public procedure max(a,b); // 這只是宣告而已 { Public declarations } end; var Form1: TForm1; implementation // 以下是實作區 真正執行的程式碼在此 {$R *.DFM} procedure TForm1.max(a,b); var a,b:inteter; begin if a>b then max(a,b):=a else max(a,b):=b; end; end. Delphi初學者,急盼指教! |
6925251
一般會員 ![]() ![]() 發表:7 回覆:14 積分:4 註冊:2003-07-09 發送簡訊給我 |
|
Chance36
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:31 回覆:1033 積分:792 註冊:2002-12-31 發送簡訊給我 |
6925251 你好
編譯不通過問題是在程序內部的錯誤,不是宣告在那個區段發生的,一直注 意你的題目,卻忽略了程式內容;另外你這個應該定義為Function才對吧!我就 直接改了。 unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } public function max(a,b:Integer):Integer; // 這只是宣告而已 { Public declarations } end; var Form1: TForm1; implementation // 以下是實作區 真正執行的程式碼在此 {$R *.DFM} procedure TForm1.max(a,b:Integer):Integer; begin // 傳回值的變數用Result或Function的名稱皆可,視個人習慣使用 if a>b then Result := a else max :=b; end; end._______________________________________ 深藍的魚,祝您好運..........連.連 _______________________________________ 深藍的魚,祝您好運..........連.連 |
bigdogchina
版主 ![]() ![]() ![]() ![]() ![]() 發表:238 回覆:523 積分:312 註冊:2003-04-28 發送簡訊給我 |
大家別理我,我是來插花的
unit Unit1; //文件定義:一個名爲Unit1的單元文件 interface //在這個部分聲明可供其他單元使用的變數,常量,函數或過程 uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs; type //聲明類型 TForm1 = class(TForm) private procedure ShowInfo(Info: String); public end; procedure ShowInfo(Info: String); var //聲明變數 Form1: TForm1; implementation //在這個部分完成單元的私有聲明,並實現interface聲明的類,函數和過程 uses //implementation部分的uses內容只針對implementation有效,interface不需要而implementation需要的單元應該在這裏引用 SysUtils, Variants; var //implementation部分可以和interface部分一樣聲明 ObjectList: TObjectList; {$R *.dfm} //編譯對應的dfm文件 procedure ShowInfo(Info: String); //實現interface部分聲明的函數或過程 begin ShowMessage(Info); end; procedure TForm1.ShowInfo(Info: String); //實現interface部分聲明的類 begin ShowMessage(Info); end; initialization //單元初始化部分 ObjList := TObjectList.Create; finalization //單元終止部分 FreeAndNil(ObjList); end.================================= 人生在勤,不索何獲 業精於勤荒於嬉,行成於思毀於隨 臨淵羡魚不如退而結網
------
人生在勤,不索何获? |
6925251
一般會員 ![]() ![]() 發表:7 回覆:14 積分:4 註冊:2003-07-09 發送簡訊給我 |
Chance36大哥,這樣編譯不通過,您在試試?
錯誤提示"procedure cannot have a result type
此行編譯無法通過procedure TForm1.max(a,b:Integer):Integer;
切盼您的答案.
unit Unit1; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type
TForm1 = class(TForm)
private
{ Private declarations }
public
function max(a,b:Integer):Integer;
{ Public declarations }
end; var
Form1: TForm1; implementation
procedure TForm1.max(a,b:Integer):Integer;//錯誤處
begin
// 傳回值的變數用Result或Function的名稱皆可,視個人習慣使用
if a>b then
Result := a
else
result :=b;
end; {$R *.DFM} end. Delphi初學者,急盼指教!
------
吳江鵬 |
shinhrn
中階會員 ![]() ![]() ![]() 發表:54 回覆:165 積分:83 註冊:2002-06-05 發送簡訊給我 |
引言: Chance36大哥,這樣編譯不通過,您在試試? 錯誤提示"procedure cannot have a result type 此行編譯無法通過procedure TForm1.max(a,b:Integer):Integer; 切盼您的答案. unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } public function max(a,b:Integer):Integer; { Public declarations } end; var Form1: TForm1; implementation procedure TForm1.max(a,b:Integer):Integer;//錯誤處 begin // 傳回值的變數用Result或Function的名稱皆可,視個人習慣使用 if a>b then Result := a else result :=b; end; {$R *.DFM} end. Delphi初學者,急盼指教!procedure TForm1.max(a,b:Integer):Integer;//錯誤處 因該是 function TForm1.max(a,b:Integer):Integer; 才對吧 |
6925251
一般會員 ![]() ![]() 發表:7 回覆:14 積分:4 註冊:2003-07-09 發送簡訊給我 |
是應該用function;還有一問題,如果調用該函數時,如何直接調用其傳回的result?
我用下面程序,可以達到結果,好像太麻煩,是否有更好的方法?
如showmessage(result)?
procedure TForm1.Button1Click(Sender: TObject);
begin
max(strtoint(edit1.text),strtoint(edit2.text));
showmessage(floattostr(max(strtoint(edit1.text),strtoint(edit2.text))));
end; Delphi初學者,急盼指教!
------
吳江鵬 |
deity
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:90 回覆:876 積分:678 註冊:2003-05-09 發送簡訊給我 |
6925251 您好:
首先让我们一起来了解函数(FuncTion)与过程(procedure)的区别:
函数能返回计算结果,即有一个返回值,而过程没有;
好了,现在对于您的问题可以下面写法(其实上面前辈都已说得很明白,小弟我纯属乱PO
|
deity
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:90 回覆:876 積分:678 註冊:2003-05-09 發送簡訊給我 |
引言: 是應該用function;還有一問題,如果調用該函數時,如何直接調用其傳回的result? 我用下面程序,可以達到結果,好像太麻煩,是否有更好的方法? 如showmessage(result)? procedure TForm1.Button1Click(Sender: TObject); begin max(strtoint(edit1.text),strtoint(edit2.text)); showmessage(floattostr(max(strtoint(edit1.text),strtoint(edit2.text)))); end; Delphi初學者,急盼指教!6925251您好: 可参考上面过程的写法: 然后直接在Button1的OnClick事件中写: procedure TForm1.Button1Click(Sender: TObject); //过程的使用 begin Pmax(strtoint(edit1.Text),strtoint(edit2.Text)); end; 即可,试试看 |
6925251
一般會員 ![]() ![]() 發表:7 回覆:14 積分:4 註冊:2003-07-09 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |