I have a silly question (it sounds silly enough at least), how do you de-allocate heap memory?
I've looked online which has directed me to Dispose or GC.Collect() but I just want to clean up memory that I've used for objects such as textBoxes.
Printable View
I have a silly question (it sounds silly enough at least), how do you de-allocate heap memory?
I've looked online which has directed me to Dispose or GC.Collect() but I just want to clean up memory that I've used for objects such as textBoxes.
You don't; the GC handles that for you. Just make sure you don't have any references to what you want to clean up, and the GC will (one day, hopefully) take care of it.
Weird.
So what if I call new twice on the same reference... see the following code
I notice that no warnings or errors but are there any problems behind the scenes?Code:TextBox variable = new TextBox();
variable = new TextBox();
Using sender to represent code-based changes in event handlers is problematic and does not really follow the design of Windows Forms. However since Microsoft's solution is not much better I'm open to anything that works.
There are some exceptions with objects that need to call Dispose() to be deallocated.
You can google about it to learn more.
Think of this code:
The b.Text line will call again the text event, looping forever. The sender will always be the same TextBox, except if I am missing something. To solve this you will need to use a boolean like:Code:private void textBox_TextChanged(object sender, EventArgs e)
{
// probably sent from code
if(sender == null)
{
// do code-wise stuff
}
else
{
TextBox b= sender as TextBox;
b.Text = "You just changed me";
}
}
So basically you temporarily switch off the event.Code:bool enableChange = true;
private void textBox_TextChanged(object sender, EventArgs e)
{
if (!enableChange) return;
// probably sent from code
if(sender == null)
{
// do code-wise stuff
}
else
{
TextBox b= sender as TextBox;
enableChange = false;
b.Text = "You just clicked me";
enableChange = true;
}
}
}
You could also use a line like
[/CODE]Code:
b -= textBox_TextChanged;
b.Text = "You just clicked me";
b += textBox_TextChanged;
Which is probably slower, but a bit more straightforward, since it blocks the event for that TextBox only.