What you want to do is create a singleton form!

We use singleton classes all the time but never a singleton form, anyway,
below is included the basic structure to the singleton class that we use.
There are various ways to impliment this pattern though.  Looking at it I
think that it should be able to be used with a form, maybe with a couple of
changes/modifications.

Whenever you want to do something with the class you then never have to
create an instance of it in the code that references it.  You access it by
going TSingleton.Instance.MyFunct or in your case -
TSingletonForm.Instance.Show and maybe a later TSingletonForm.Instance.Hide
or .Release


I hope that my ramblings are what your are after.

Nahum




unit Singleton;

interface

uses
  Classes;  // I think that this is what is required here.

type
{--------------------------------------------}

  TSingleton = class;

{--------------------------------------------
    SINGLETON
 --------------------------------------------}

  TSingleton = class(TObject)
  private
  protected
    constructor CreateInstance;

  public
    constructor Create;
    class function Instance : TSingleton;
    destructor Destroy; override;

  end;

{--------------------------------------------}

implementation

var
  TheSingleton : TSingleton;

{--------------------------------------------
    SINGLETON
 --------------------------------------------}

constructor TSingleton.Create;
begin
  assert(False, 'FOOLISH MORTAL, thou shalt not call singleton constructors
directly!!');
end;

constructor TSingleton.CreateInstance;
begin
  inherited Create;

// do whatever you would normally do in the constructor

end;

class function TSingleton.Instance : TSingleton;
begin

  if not assigned(TheSingleton)
    then TheSingleton := TSingleton.CreateInstance;

  Result := TheSingleton;

end;

destructor TSingleton.Destroy;
begin
  inherited Destroy;

  TheSingleton := nil;
end;

{--------------------------------------------}

initialization
  TheSingleton := nil ;

finalization
  TheSingleton.Free;

{--------------------------------------------}

end.




-----Original Message-----
From: Gnad, Philip [mailto:[EMAIL PROTECTED]]
Sent: Friday, 27 August 1999 07:50
To: Multiple recipients of list database
Subject: [DUG-DB]: Dumb Question - Test if form created


Just a real easy one  - 

I want to test whether any instance of form has already been created and if
not create an instance. I can always try a Form.Show and trap for the access
violation, but there must be a more elegant way of doing this.

Phil

Phil Gnad
Analyst Programmer
MIS Development
Christchurch City Council
New Zealand
Phone (03) - 371-1859
Fax     (03) - 371-1989

---------------------------------------------------------------------------
  New Zealand Delphi Users group - Database List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
---------------------------------------------------------------------------
  New Zealand Delphi Users group - Database List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to