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.
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.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
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.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
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.