This may sound like a dumb question, but I am hoping someone might comment
on it....

Consider the following method (assuming that Form1 is a class derived from
System.Windows.Forms.Form) :

private void MyMethod()
{
int i = 1;
Form1 myForm = new Form1();

myForm.Show();
}

Both of the variables, i and myForm, are local variables that should go out
of scope when the method is finished executing. But the myForm variable
continues to live on. Is this correct? Why is the scope of form variables
different than the scope of other types?

Re: Scope of form variables... by Jon

Jon
Tue Nov 25 02:58:40 CST 2003

craig <e@mail.com> wrote:
> This may sound like a dumb question, but I am hoping someone might comment
> on it....
>
> Consider the following method (assuming that Form1 is a class derived from
> System.Windows.Forms.Form) :
>
> private void MyMethod()
> {
> int i = 1;
> Form1 myForm = new Form1();
>
> myForm.Show();
> }
>
> Both of the variables, i and myForm, are local variables that should go out
> of scope when the method is finished executing. But the myForm variable
> continues to live on. Is this correct? Why is the scope of form variables
> different than the scope of other types?

It's not. The variable itself doesn't live on. However, the form
continues to be shown until it's closed etc and there are no other live
references to it. (The UI system will have a reference to it until it's
closed.)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Scope of form variables... by craig

craig
Tue Nov 25 09:08:11 CST 2003

I see. So the "myForm" reference goes out of scope, but the system
maintains its own reference to the form, thus keeping it alive?

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a2d2a1c13f56ea989b32@msnews.microsoft.com...
> craig <e@mail.com> wrote:
> > This may sound like a dumb question, but I am hoping someone might
comment
> > on it....
> >
> > Consider the following method (assuming that Form1 is a class derived
from
> > System.Windows.Forms.Form) :
> >
> > private void MyMethod()
> > {
> > int i = 1;
> > Form1 myForm = new Form1();
> >
> > myForm.Show();
> > }
> >
> > Both of the variables, i and myForm, are local variables that should go
out
> > of scope when the method is finished executing. But the myForm variable
> > continues to live on. Is this correct? Why is the scope of form
variables
> > different than the scope of other types?
>
> It's not. The variable itself doesn't live on. However, the form
> continues to be shown until it's closed etc and there are no other live
> references to it. (The UI system will have a reference to it until it's
> closed.)
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Re: Scope of form variables... by Jon

Jon
Tue Nov 25 09:12:49 CST 2003

craig <e@mail.com> wrote:
> I see. So the "myForm" reference goes out of scope, but the system
> maintains its own reference to the form, thus keeping it alive?

Yes. Which is what you want, really - you wouldn't want to start
showing the form and then it disappear at the end of the method, would
you?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Scope of form variables... by craig

craig
Tue Nov 25 10:17:30 CST 2003

No, that is not what we would want. But it seemed to me that that is
exactly what should happen unless the variable reference was defined with a
glodal scope rather than a local scope. Knowing that this is not what
hapened lead me to wonder why.

I have been searching MSDN and the literature for information on windows
forms lifecycle, including what happens when forms are created and
destroyed, the order of events, etc., but suprisingly, I can't seem to find
very much.

I really appreciate your input. Thanks!



"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a2d81c464f6ef63989b39@msnews.microsoft.com...
> craig <e@mail.com> wrote:
> > I see. So the "myForm" reference goes out of scope, but the system
> > maintains its own reference to the form, thus keeping it alive?
>
> Yes. Which is what you want, really - you wouldn't want to start
> showing the form and then it disappear at the end of the method, would
> you?
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Re: Scope of form variables... by Paul

Paul
Thu Dec 04 17:16:44 CST 2003

Put simply -

i - int is a value type so it lives on the stack, and is cleaned up on exit.
myForm - Form is a reference type so it effectively lives until its not
referenced by anything and is garbage collected (similar to heap allocations
in C++ - but they are automatically deleted for you when no longer needed).

Paul

"craig" <e@mail.com> wrote in message
news:elh8YLwsDHA.1788@tk2msftngp13.phx.gbl...
> This may sound like a dumb question, but I am hoping someone might comment
> on it....
>
> Consider the following method (assuming that Form1 is a class derived from
> System.Windows.Forms.Form) :
>
> private void MyMethod()
> {
> int i = 1;
> Form1 myForm = new Form1();
>
> myForm.Show();
> }
>
> Both of the variables, i and myForm, are local variables that should go
out
> of scope when the method is finished executing. But the myForm variable
> continues to live on. Is this correct? Why is the scope of form
variables
> different than the scope of other types?
>
>
>