The solution to that is to override the Create methode and have it fire an
assert automatically such as...

constructor TMyGadget.Create;
begin
  assert(False, 'Oi! you can't do this, use TMyGadget.CreateBriefly
instead.');
end;

Then create a new protected constructor that the class procedure
CreateBriefly uses...

constructor TMyGadget.HiddenCreate;
begin
  inherited Create;
  // do stuff
end;

class procedure TMyGadget.CreateBriefly;
var it:TMyGadget;
begin
  it:=nil;
  try
    it:=TMyGadget.HiddenCreate();  //call the protected constructor
  finally
    it.free;
  end;
end;

This way TMyGadget can only be created/used with the CreateBriefly class
procedure and anyone that tries to use it in the traditional sense, using
TMyGadget.Create, will find out pretty quickly how they should be using it.
:)



Nahum

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
> Behalf Of Aaron Scott-Boddendijk
> Sent: Friday, October 27, 2000 10:38
> To: Multiple recipients of list delphi
> Subject: Re: [DUG]: Free inside Create
>
>
> > You missed the 'class' part of the class procedure, it should
> be as below
> > with the class reference at the very begining of the statement.
>  Good idea
> > though, you get rid of the object trying to free itself within its own
> > constructor which sounds a bit dodgy. :)
>
> > class procedure TMyGadget.CreateBriefly;
> > var it:TMyGadget;
> > begin
> >   it:=nil;
> >   try
> >     it:=TMyGadget.Create();  //call the protected constructor
> >   finally
> >     it.free;
> >   end;
> > end;
>
> The downside is that the 'protected' feature isn't possible as
> create is public in TObject.
> Means that an outsider using your class can still unknowingly do
> a TMyGadet.Create even
> if they're not a descendant.
>
> --
> Aaron Scott-Boddendijk
> INTAZ Limited
> +64 7 838 3371 Voice
> +64 7 838 3372 Fax
>
>
> ------------------------------------------------------------------
> ---------
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
>
>

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to