Einzelnen Beitrag anzeigen

Benutzerbild von littleDave
littleDave

Registriert seit: 27. Apr 2006
Ort: München
556 Beiträge
 
Delphi 7 Professional
 
#4

AW: Package - Unit.Initialization - Reihenfolge

  Alt 10. Mär 2011, 12:21
Bevor ich krampfhaft versuchen würde, die Reihenfolge "vorherzusehen", würde ich den Zugriff auf die Instanz über eine Methode kapseln (ob die Syntax so in Delphi XE geht, weiß ich nicht - aber ich denke, es sollte klar sein)

Delphi-Quellcode:
unit UnitA;

type
  MyInstances = class
  private
    class var FInstance : MyInstances;
  protected
    class function GetInstance: MyInstance;
  public
    class property Instance : MyInstances read GetInstance;

    procedure RegisterInstance(AInstance: TObject);
  end;

class function MyInstances.GetInstance: MyInstance;
begin
  // check if global instance variable is initialized
  if MyInstance.FInstance = nil then
     // not initialized, so create
     MyInstance.FInstance := MyInstance.Create;
  result := MyInstance.FInstance;
end;

procedure MyInstances.RegisterInstance(AInstance: TObject);
begin
  // do sth. with AInstance
end;

initialization
  // nothing needed here

finalization
  // clean up
  MyInstances.FInstance.Free;
Delphi-Quellcode:
unit UnitB;

uses
  UnitA;

procedure Test;
begin
  MyInstances.Instance.RegisterInstance(TObject.Create);
end;
Jabber: littleDave@jabber.org
in case of 1 is 0 do external raise while in public class of object array else repeat until 1 is 0
  Mit Zitat antworten Zitat