Monday, 12 August 2013

Scope of a reference variable in an Event Handler C#

Scope of a reference variable in an Event Handler C#

I have a simple form with a button control on. When clicked, a checked
checkbox appears.
I'm a beginner and wanted to know what happens to my reference variable
myCheckbox and what happens to the checkbox object it is pointing to on
the heap when the event Handler is out of its lifetime ?
If the checkbox object on the heap exists still can I access it by
pointing another reference to it ?
Thank You
Lee
My code -
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CheckBox myCheckbox = new CheckBox();
myCheckbox.Name = "My Check Box";
myCheckbox.Checked = true;
myCheckbox.Location = new Point(500, 150);
this.Controls.Add(myCheckbox);
}
}
}

No comments:

Post a Comment