Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DELPHI 一問?

0 views
Skip to first unread message

鮑魚

unread,
Feb 27, 1997, 3:00:00 AM2/27/97
to

設 Source:TObject;
那 Memo1.Color=(Source as Tlabe1).Color;
是什麼意思? 其中的 Source as Tlabe1 又是何種解釋? 和 Source is Tlabe1 差
在那裡呢? 請各位高手解釋一下?

--
☆冷凍鮑魚切丁☆
E-Mail:aba...@ms8.hinet.net
--
* Origin: ★ 交通大學資訊科學系 BBS ★ <bbs.cis.nctu.edu.tw: 140.113.23.3>

葛瑞菲

unread,
Feb 28, 1997, 3:00:00 AM2/28/97
to

==> 在 progr...@bbs.cis.nctu.edu.tw (鮑魚) 的文章中提到:
: 設 Source:TObject;

: 那 Memo1.Color=(Source as Tlabe1).Color;
: 是什麼意思? 其中的 Source as Tlabe1 又是何種解釋? 和 Source is Tlabe1 差
: 在那裡呢? 請各位高手解釋一下?

As 是具有檢查能力的 TypeCasting Operator ;
Is 則是在 Run-Time 時檢查某一個 Object 的形態,也是 Delphi RTTI ( Run Time
Type Information) 的最重要的 Operator.

比如說:
procedure TForm1.Panel1DragDrop(Sender, Source: TObject; X, Y: Integer);

begin
if source is TButton then (source as TButton).Caption:='Hello World';
end;

procedure TForm1.Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);

if source is TButton then (source as TButton).Caption:='Hello World';
end;

procedure TForm1.Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept:=True;
end;

當某一個 Component 被 Drag&Drop 到 Panel1 時,Event Procedure Panel1DragDrop
便會以 Is 判斷是否為 Button , 倘若是,則利用 As 做 TypeCasting 讓原本是
TObject 形態的 Source , TypeCasting 成為 TButton, 並且將 Drop 進來的 Button
的 Caption 改便成為 "Hello World"

如果您已經確知資料形態無誤,不多做檢查的下面寫法比較有效率:
if source is TButton then TButton(source).Caption:='Hello World';

--
歡迎加入免費的 DelphiChat Mailing List,加入辦法請參見:
32 Bit Delphi 深度歷險 : http://www.aaa.hinet.net/delphi
http://www.kpi.edu.tw/delphi http://ibmsrv.cc.nthu.edu.tw/DELPHI

0 new messages