全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2037
推到 Plurk!
推到 Facebook!

Property,read,write的困惑

缺席
alex_soong
一般會員


發表:2
回覆:3
積分:1
註冊:2004-11-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-01 14:16:31 IP:218.2.xxx.xxx 未訂閱
我是初次寫VCL檔,可是在property時,read和write總是會報如下錯誤: Incompatible types 察看幫助,得到如下結果: This message is displayed when you try to use a var parameter in a property setter parameter. The parameter of a property setter procedure cannot be a var or out parameter. 雖然,我能理解字面的意思,但是實在是無法做出正確的理解。 我的檔案如下: FADODatabase = class(TComponent) private {private declarations here} fFilePathandName: string; fFilepath: string;//數據庫路徑 fFileName: string;//數據庫表名 fTableNames: Tstrings; //數據庫表名的列表 fTablecount: integer; //表名個數 fTableIndex: integer; //工作的指針 fBExists: boolean; //檢測數據庫是否存在 Procedure SetFilePath(FilePathAndName:string); procedure SetFileName(FilePathAndName:string); Procedure SetDatabaseExists(FilePathAndName:string); procedure CreateDatabase(FilePathAndName:string); function SetTableNames(FilePathAndName:string): Tstrings; procedure SetTableNamesCount; Protected {protected declarations here} public {public declarations here} Property FilePathAndName: string read fFilePathandName write fFilePathandName; property FilePath: string read fFilepath write SetFilePath; Property Filename: string read fFileName Write SetFileName; Property BFileExists: boolean read fBExists;// Write SetDatabaseExists; property TableNameList: Tstrings read fTableNames;// Write SetTableNames; property Tablenamecount: integer read constructor Create(AOwner: TComponent); override; destructor Destroy; override ; published {published declarations here} end; 在我寫到Property BFileExists: boolean read fBExists;// Write SetDatabaseExists;時就會報以上錯誤。 謝謝各位
wzw_2005
一般會員


發表:1
回覆:4
積分:1
註冊:2005-05-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-01 15:32:39 IP:222.216.xxx.xxx 未訂閱
property的声明应该放到published下,而不是放在public下。。。。
alex_soong
一般會員


發表:2
回覆:3
積分:1
註冊:2004-11-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-06-01 16:10:18 IP:218.2.xxx.xxx 未訂閱
我已經知道自己的錯誤了,是我對read和write的行爲上的理解有錯誤。write就是應該設置這個變量,而不是獲取這個變量,是我意識上的錯誤。如,應該寫成Property BFileExists: boolean read fBExists Write Setboolean; procedure Setboolean(value: boolean); 這樣就能通過了。 property不一定非要寫在published裏面。
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-06-01 21:12:29 IP:61.31.xxx.xxx 未訂閱
做一點簡短的補充:
一般屬性的寫法為
Property BFileExists: boolean read fBExists Write fBExists Default false;
看起來很簡單。讀就是讀。寫就是寫。不做任何處理。    Property BFileExists: boolean read GetBExists Write fBExists;    Function GetBExists:Boolean;
begin
  Result :=  FileExists(....);
end;
當讀取該元件屬性時,會執行 GetBExists。
用來確認屬性的正確性。例如:TLabel 的 GetTransparent    Property BFileExists: boolean read fBExists Write WriteBExists;    Procedure WriteBExists(Value:Boolean);
begin
  if fBExists<>Value then
  begin
    ...
  end;
end;
通常這樣的用意在於,寫入該屬性時。
可以檢查屬性的有效值或觸發異動屬性的事件。做一些處理。
像是刷新元件等 Repaint。 
~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~
系統時間:2024-06-29 22:35:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!