Scope of variable
-
I know this is a simple question but I would like to know How and Why this works; Lets say I have two forms :
Form1
andForm2
whereForm1
is the main form. I am creating aForm2
variable by clicking a button inForm1
as seen in the code piece below.private void button1_Click(object sender, EventArgs e)
{
Form2 myForm2 = new Form2();
myForm2.Show();
}Why am I able to see form2. Because
myForm2
is a local variable and just after it is shown, the functionbutton1_Click
terminates. So I mustn't be able to see Form2. NOTE-1 : If you try the same with MS VC++ you will not be able to seeForm2
. NOTE-2 : I know in .NET variables are not deleted when they are out out of scope. Garbage collector does it. Bu in an other function I triedGC.Collect();
which forces the garbage collector butForm2
still remains, it is not deleted. Regards -
I know this is a simple question but I would like to know How and Why this works; Lets say I have two forms :
Form1
andForm2
whereForm1
is the main form. I am creating aForm2
variable by clicking a button inForm1
as seen in the code piece below.private void button1_Click(object sender, EventArgs e)
{
Form2 myForm2 = new Form2();
myForm2.Show();
}Why am I able to see form2. Because
myForm2
is a local variable and just after it is shown, the functionbutton1_Click
terminates. So I mustn't be able to see Form2. NOTE-1 : If you try the same with MS VC++ you will not be able to seeForm2
. NOTE-2 : I know in .NET variables are not deleted when they are out out of scope. Garbage collector does it. Bu in an other function I triedGC.Collect();
which forces the garbage collector butForm2
still remains, it is not deleted. RegardsHi, a variable is visible ("in scope") as long as you are within the first pair of brackets {} that encloses its declaration, so myForm2 lives only inside your button1_Click method. The Form it refers to is an object, calling Show() makes it visible AND keeps it alive, even when button1_Click returns and hence variable myForm2 fades away. Once visible, the Form does not need the variable, it has its own life. You would have to close the Form somehow (by clicking a Close box, or by calling the Close() method) to end its useful life and make it collectable. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
Hi, a variable is visible ("in scope") as long as you are within the first pair of brackets {} that encloses its declaration, so myForm2 lives only inside your button1_Click method. The Form it refers to is an object, calling Show() makes it visible AND keeps it alive, even when button1_Click returns and hence variable myForm2 fades away. Once visible, the Form does not need the variable, it has its own life. You would have to close the Form somehow (by clicking a Close box, or by calling the Close() method) to end its useful life and make it collectable. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
Thats the point; "Once visible, the Form does not need the variable". I just can't understand why and how?, sorry :) But thats not the case in MS VC++. Once you create a dialog class and if the object is out of scope, the form is also deleted. Right? There are strange things going behind c# :)
-
Thats the point; "Once visible, the Form does not need the variable". I just can't understand why and how?, sorry :) But thats not the case in MS VC++. Once you create a dialog class and if the object is out of scope, the form is also deleted. Right? There are strange things going behind c# :)
zafersavas wrote:
There are strange things going behind c#
Oh no, if anything is strange it is in the C++ world. A visible Form remains alive without a reference; same is true for a file you create on disk, it does not get deleted when your file variable vanishes. BTW dialogs get shown using ShowDialog() which does not return until the Dialog gets closed. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
I know this is a simple question but I would like to know How and Why this works; Lets say I have two forms :
Form1
andForm2
whereForm1
is the main form. I am creating aForm2
variable by clicking a button inForm1
as seen in the code piece below.private void button1_Click(object sender, EventArgs e)
{
Form2 myForm2 = new Form2();
myForm2.Show();
}Why am I able to see form2. Because
myForm2
is a local variable and just after it is shown, the functionbutton1_Click
terminates. So I mustn't be able to see Form2. NOTE-1 : If you try the same with MS VC++ you will not be able to seeForm2
. NOTE-2 : I know in .NET variables are not deleted when they are out out of scope. Garbage collector does it. Bu in an other function I triedGC.Collect();
which forces the garbage collector butForm2
still remains, it is not deleted. RegardsThe variable contains a reference to the Form instance, a copy of that reference gets passed on to the Window Manager (or whatever), at which point your app and the whatever both refer to the instance, even when your reference is destroyed the whatever maintains its reference.
-
Thats the point; "Once visible, the Form does not need the variable". I just can't understand why and how?, sorry :) But thats not the case in MS VC++. Once you create a dialog class and if the object is out of scope, the form is also deleted. Right? There are strange things going behind c# :)
That's because the Form class is just a wrapper in the .Net runtime environment to manage a Windows API window handle. Creating a form and calling its Show method creates a window handle, and the window handle will remain until it is released (which happens when the form's Dispose method is called, be it from code or by Garbage Collecgtion). All these .Net classes handling windows API (or COM interop, which is even worse) have been designed to make life easy as long as you don't think about it too much. Soon as you start thinking it looks illogical and only reveals its logic when you read up on the System.Runtime.InteropServices namespace. Being an old C hand you'll know much better than I what happens to window handles and things when they're not released. All I know is that when I detect a memory leak I have to look into the interop hell unless I can find an easy way (within .Net) to clean things up. That's twe price we pay for using a comfortable top level language like C# instead of ATL and MFC.
-
I know this is a simple question but I would like to know How and Why this works; Lets say I have two forms :
Form1
andForm2
whereForm1
is the main form. I am creating aForm2
variable by clicking a button inForm1
as seen in the code piece below.private void button1_Click(object sender, EventArgs e)
{
Form2 myForm2 = new Form2();
myForm2.Show();
}Why am I able to see form2. Because
myForm2
is a local variable and just after it is shown, the functionbutton1_Click
terminates. So I mustn't be able to see Form2. NOTE-1 : If you try the same with MS VC++ you will not be able to seeForm2
. NOTE-2 : I know in .NET variables are not deleted when they are out out of scope. Garbage collector does it. Bu in an other function I triedGC.Collect();
which forces the garbage collector butForm2
still remains, it is not deleted. RegardsWhen ever you create a form and show it, a reference to the form is added internaly. There is a property of Application Object named "OpenForms" which returns a read only FormsCollection containing all the forms opened by the application. Since a reference is held to the form object even though your local variable is collected by gc, the form does not get collected. You can access the form through that collection and call the close method on it to close the form. Then the form is removed from the OpenForms collection and will be collected by the gc. HTH
Nirandas, a developer from India. http://www.nirandas.com
-
When ever you create a form and show it, a reference to the form is added internaly. There is a property of Application Object named "OpenForms" which returns a read only FormsCollection containing all the forms opened by the application. Since a reference is held to the form object even though your local variable is collected by gc, the form does not get collected. You can access the form through that collection and call the close method on it to close the form. Then the form is removed from the OpenForms collection and will be collected by the gc. HTH
Nirandas, a developer from India. http://www.nirandas.com
Thanks. I understand now; You can reach to the form by
Application.OpenForms[1]
. This makes sense :)