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;



Cheers,

Nahum

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
> Behalf Of Nello Sestini
> Sent: Thursday, October 26, 2000 21:50
> To: Multiple recipients of list delphi
> Subject: Re: [DUG]: Free inside Create
>
>
> a Delphi constructor (unlike those in C++) doesn't get called
> implicitly (like for local variables or for args passed by value)
>
> This means that to create one of these things you'll have to
> explicitly call TMyGadget.Create  (or use new ).
>
> So how about writing a class function CreateBriefly  that isn't
> a constructor and call that instead.    Make CreateBriefly
> public and make the real Create constructor protected.
>
> CreateBriefly will look like something like:
>
>        procedure TMyGadget.CreateBriefly;
>        var it:TMyGadget;
>        begin
>           it:=nil;
>           try
>             it:=TMyGadget.Create();  //call the protected constructor
>           finally
>             it.free;
>           end;
>        end;
>
> then in all the places you currently call Create you
> change these to calls to CreateBriefly.
>
>
> Personally i think "doing stuff" other than just getting the object
> properly initialised (like running a process) in a constructor is
> bad design.
>
> But I suppose that's debatable.
>
> -ns
>
>
>
>
>
>
>
> -----Original Message-----
> From: Rohit Gupta <[EMAIL PROTECTED]>
> To: Multiple recipients of list delphi <[EMAIL PROTECTED]>
> Date: Thursday, October 26, 2000 15:32
> Subject: [DUG]: Free inside Create
>
>
> Whats the recommended way of executing Free (or Destroy) inside the
> constructor ?
>
> I have been using Free inside teh constructor which has worked under
> Delphi3.  Under Delphi 5, it karks it.  Looking at the code, the idea now
> seems naive.
>
> BUT, I still want to do it.  The object is like a process,  all
> the work is
> done in the constructor, after which its simples fot it to commit
> hara-kiri,
> preferably a clean one.
>
> Executing an abort inside the constructor calls the destructor,
> unfortunately that leaves an unhandled exception.  Having about
> 20 of these
> beasts, I want the code to look simple and be foolproof.
>
>
>
> ------------------------------------------------------------------
> ---------
>     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"
>
>

---------------------------------------------------------------------------
    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