[DUG] Variables stored
Jolyon Smith
jsmith at deltics.co.nz
Fri Jan 21 20:18:19 NZDT 2011
> you might be able to go even one better by setting the Action = caFree
This is hardly ever a good idea when a form is intended to be used as a
dialog as it is a frequent pattern to pass information back from the dialog
to the invoking code via public properties of the dialog form class:
Dlg := TSomeDialog.Create(NIL);
Try
If dlg.ShowMOdal = mrOK then
Begin
FileName := dlg.FileName;
// blah blah
End;
Finally
Dlg.Free;
End;
Note the use of Free in this context is safe. "Release" is advised if you
Free a form in response to some event on the form itself, e.g. in an OnClick
of a button. This avoids the form being destroyed out from underneath the
feet of the code in the event handler.
But when you have invoked a form using ShowModal, ShowModal will not return
until the message loop created for the form has terminated. If the form
could be freed from out underneath the feet of this code then you are in
serious trouble.
In general, it is a bad idea to have a situation that could result in a form
being free'd in response to an event on that form. When I see "Release()"
being used I am immediately nervous.
> Actually is Self.Create safe - rather than TMyDialog.Create?
Much safer imho.
1) if you renamed your class your method body doesn't need to change. Yeah
you can use a "refactoring" which will take care of such details, but why
rely on tool support *more* than you strictly need to ? (and see previous
comments about dubious reliability of ref[a/u]ctorings)
2) if you user poor man's code re-use and copy/pasted this method (perhaps
you have another form where you want a class "Execute()" method, then if you
have hard-wired the class name in the method body, and if you forget to fix
it in your pasted code (happens ALL too often!) then your second dialog
class is going to instantiate the wrong form class, which could have you
banging your head for a while until you realise your mistake.
3) The "self" is itself I think redundant.
More information about the Delphi
mailing list