2014-02-22 52 views
0

我有一個TObject調用Target2這是一個指針說TLabel並希望從此指針獲取屬性Name。因此,我有這個獲取指針的成員

Procedure TGetName() 
var 
    Item : TLabel; 
Begin 
    if Target2 is TLabel then 
    begin 
    Item := Target2; 
    if Item.Name := 'SomeName' then 
     begin 
     .... 
     dosomething(); 
     end; 
    end; 
end; 

但它似乎毫無意義有Item現在變成了指針的指針,但是當我這樣做:

Procedure TGetName() 
Begin 
    if Target2 is TLabel then 
    begin 
     if Target2.Name := 'SomeName' then 
     begin 
      .... 
     dosomething(); 
     end; 
    end; 
end; 

我得到errror這個名字是不是Target2成員。因此,如何在不創建指向指針Target2的另一個指針的情況下訪問它?

+2

試試這個'(Target2 as TLabel).Name:='SomeName'' – Iqbal

+0

你在說什麼? – MBo

+0

感謝lqbal的工作,@MBo我在談論問題中的兩個指針,但只想要1. Target2'(我想要)'Item'(我不想要) –

回答

3

你只需要施放。如果你準備斷言Target2TLabel然後用檢查轉換:

var 
    Lbl: TLabel; 
.... 
Lbl := Target2 as TLabel; 

使用is否則檢查,然後未經檢查的轉換是罰款:

if Target2 is TLabel then 
begin 
    Lbl := TLabel(Target2); 
    .... 

你並不需要摧毀因爲它只是對其他人擁有的對象的引用。