Einzelnen Beitrag anzeigen

choose

Registriert seit: 2. Nov 2003
Ort: Bei Kiel, SH
729 Beiträge
 
Delphi 2006 Architect
 
#2

Re: Property TFont in neuer Kompo klappt nicht

  Alt 23. Dez 2003, 13:44
Wenn Du das direkte Beschreiben einer Exemplarvariablen zulässt, ist die Referenz auf das zuvor erzeugte Exemplar verloren. Darüber hinaus ist nicht klar, wo das zugewisene Exemplar letztlich wann freigegeben wird:
Delphi-Quellcode:
myFont:= TFont.Create;
try
  AnObject.Font:= myFont;
  AnotherObject.Font:= myFont;
finally
  FreeAndNil(myFont);
end;
Verwende stattdessen eine transparente Zuweisung der Form
Delphi-Quellcode:
TMyClass = class
private
  FFont: TFont;
  procedure SetFont(const AValue: TFont);
public
  property Font: TFont
    read FFont
    write SetFont;
//...

procedure TMyClass.SetFont(const AValue: TFont);
begin
  FFont.Assign(AValue);
end;
wobei Du das Exemplar hinter FFont einmalig im Konstruktor erzeugst, bzw im Destruktor zerstörst.
gruß, choose
  Mit Zitat antworten Zitat