Board index » delphi » Create form(nil) or form(self)??

Create form(nil) or form(self)??

Application is almost always the best option for creating forms.

Form2 := TForm2.Create(Application);

Quote
Marko wrote in message ...
>Hello,

>I have seen forms being created at runtime with both (nil) and (self).
>I have read the help files explanation of nil and self.

>nil denotes a pointer type constant that does not point to anything.
>Self can be used as a pointer to the instance through which the method is
>being called.

> I still do not understand when to use one over the other.   Could someone
>clear this up for me.  Thank you.

>Marko
>---------------
>m...@sepcoinc.com

>--

>Marko
>---------------
>m...@sepcoinc.com

 

Re:Create form(nil) or form(self)??


Hello,

I have seen forms being created at runtime with both (nil) and (self).
I have read the help files explanation of nil and self.

nil denotes a pointer type constant that does not point to anything.
Self can be used as a pointer to the instance through which the method is
being called.

 I still do not understand when to use one over the other.   Could someone
clear this up for me.  Thank you.

Marko
---------------
m...@sepcoinc.com

--

Marko
---------------
m...@sepcoinc.com

Re:Create form(nil) or form(self)??


Well...ok...why?

I always use nil and have never had any ill effects. I'm not sure why
I use nil, other than I didn't know what to use and nil seems to work.
Could you clarify why Application is best?

Scott

Quote
Sean Hamilton wrote in message ...
>Application is almost always the best option for creating forms.

>Form2 := TForm2.Create(Application);

>Marko wrote in message ...
>>Hello,

>>I have seen forms being created at runtime with both (nil) and
(self).
>>I have read the help files explanation of nil and self.

>>nil denotes a pointer type constant that does not point to anything.
>>Self can be used as a pointer to the instance through which the
method is
>>being called.

>> I still do not understand when to use one over the other.   Could
someone
>>clear this up for me.  Thank you.

>>Marko
>>---------------
>>m...@sepcoinc.com

>>--

>>Marko
>>---------------
>>m...@sepcoinc.com

Re:Create form(nil) or form(self)??


On Mon, 26 Apr 1999 08:45:30 -0500, "Scott Roberts"

Quote
<sgrob...@nospam-ionet.net> wrote:
>Well...ok...why?

>I always use nil and have never had any ill effects. I'm not sure why
>I use nil, other than I didn't know what to use and nil seems to work.
>Could you clarify why Application is best?

The AOwner parameter to TForm.Create sets the owner of the new form.
This has two major effects.  First, the new form appears in the
Components array of its owner.  Second, the form is destroyed
automatically when its owner is destroyed.  For forms, the latter is
usually more important.

If you control destruction of the form yourself, the owner may not
matter.  For example, with a modal form, if you write

  with TWeaselForm.Create(nil) do begin
    try
      ShowModal;
    finally
      Free;
    end;
  end;

if makes no practical difference whether you use nil, Self, Application,
or some random component.  On the other hand, if the new form were not
modal and the code that created it were a method of some form other than
the application's main form, and you used Self as the owner, you would
find that closing (and freeing) the owner would also destroy the owned
form.  This might just possibly be what you want; in that case, using
Self for the owner would be a useful trick.

Finally, if you use nil for the owner, a form open when the program
terminates will not be destroyed.  Sure, the form window will go away
along with everything else, but the form's destructor will never be
called.  You can see that by putting code in the form's OnDestroy event
handler.  It won't be called at termination if the form has no owner.

The startup code uses Application for the owner (via
Application.CreateForm) because it's the only object that can own forms
that the VCL can be sure exists.  The alternative, which would have
about the same effect, is the application's main form, but someone has
to own the main form--so the choice is Application.

So...nil is okay, if you take care to destroy the form yourself, or
don't mind that it might not be destroyed properly.  Self is okay if you
don't want the form to outlive its creator (and could be useful if
having it destroyed with its creator is the effect you want).
Application is always safe, which makes it a good choice if you don't
want to think through all the cases every time you create a form.

--
"Oh, shootings.  Yes, but that doesn't mean Americans are more {*word*268}
than other people.  We're just better shots."

Re:Create form(nil) or form(self)??


Thanks David, good answer. I will mend my ways. :-)

Scott

Quote
David Rifkind wrote in message

<3725918c.2927...@netnews.worldnet.att.net>...
Quote
>On Mon, 26 Apr 1999 08:45:30 -0500, "Scott Roberts"
><sgrob...@nospam-ionet.net> wrote:
>>Well...ok...why?

>>I always use nil and have never had any ill effects. I'm not sure
why
>>I use nil, other than I didn't know what to use and nil seems to
work.
>>Could you clarify why Application is best?

>The AOwner parameter to TForm.Create sets the owner of the new form.
>This has two major effects.  First, the new form appears in the
>Components array of its owner.  Second, the form is destroyed
>automatically when its owner is destroyed.  For forms, the latter is
>usually more important.

>If you control destruction of the form yourself, the owner may not
>matter.  For example, with a modal form, if you write

>  with TWeaselForm.Create(nil) do begin
>    try
>      ShowModal;
>    finally
>      Free;
>    end;
>  end;

>if makes no practical difference whether you use nil, Self,
Application,
>or some random component.  On the other hand, if the new form were
not
>modal and the code that created it were a method of some form other
than
>the application's main form, and you used Self as the owner, you
would
>find that closing (and freeing) the owner would also destroy the
owned
>form.  This might just possibly be what you want; in that case, using
>Self for the owner would be a useful trick.

>Finally, if you use nil for the owner, a form open when the program
>terminates will not be destroyed.  Sure, the form window will go away
>along with everything else, but the form's destructor will never be
>called.  You can see that by putting code in the form's OnDestroy
event
>handler.  It won't be called at termination if the form has no owner.

>The startup code uses Application for the owner (via
>Application.CreateForm) because it's the only object that can own
forms
>that the VCL can be sure exists.  The alternative, which would have
>about the same effect, is the application's main form, but someone
has
>to own the main form--so the choice is Application.

>So...nil is okay, if you take care to destroy the form yourself, or
>don't mind that it might not be destroyed properly.  Self is okay if
you
>don't want the form to outlive its creator (and could be useful if
>having it destroyed with its creator is the effect you want).
>Application is always safe, which makes it a good choice if you don't
>want to think through all the cases every time you create a form.

>--
>"Oh, shootings.  Yes, but that doesn't mean Americans are more
{*word*268}
>than other people.  We're just better shots."

Re:Create form(nil) or form(self)??


Gentlemen,

Thank you for all your wonderful answers to the above question.  Another
side question.  Is is necessary to to set the action when closing the
created form (i.e. Action = free)  or is everything cleared from memory with
the free statement?

Marko
---------------
m...@sepcoinc.com

Re:Create form(nil) or form(self)??


Im Artikel <VYxU2.70$ed3.12...@iagnews.iagnet.net>, "Marko" <m...@sepcoinc.com>
schreibt:

Quote
>I have read the help files explanation of nil and self.

That's in fact not very helpful here ;-)

The argument in the call to Create denotes the Owner of the new form. Therefore
a Nil argument creates a form with no owner, other legal arguments were Self or
Application. If Self is a form, then the new form will become a child form of
that form, perhaps a reasonable setting for MDI child forms.

If you are curious, look for the purpose of the Owner argument in Form.Create.
I cannot provide further help hereupon, since I'm not familiar with the details
myself.

DoDi

Re:Create form(nil) or form(self)??


Quote
On Mon, 26 Apr 1999 20:55:15 -0500, "Marko" <m...@sepcoinc.com> wrote:
>Thank you for all your wonderful answers to the above question.  Another
>side question.  Is is necessary to to set the action when closing the
>created form (i.e. Action = free)  or is everything cleared from memory with
>the free statement?

Depends whether you Close the form or Free it.  Close invokes the
OnCloseQuery and OnClose handlers (except for modal forms), and will by
default only hide an SDI form or minimize an MDI form, but will free it
if the OnClose handler sets Action to caFree.  Free will free the form
no matter what, and will not call OnCloseQuery or OnClose.

--
"Oh, shootings.  Yes, but that doesn't mean Americans are more {*word*268}
than other people.  We're just better shots."

Other Threads