0

I'm trying to understand how using statement is working as when I run the code below my textbox (mytextbox) is disappearing.

public partial class Form1 : Form
{
    TextBox mytextbox = new TextBox();
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(mytextbox);
        mytextbox.Validated+=new EventHandler(mytextbox_Validated);
    }
    private void mytextbox_Validated(object sender, EventArgs etxtbox)
    {
        using (TextBox myeventtxtbox = ((TextBox)sender))
        { 

        }
    }
}

I was thinking that the using statement will release myeventtxtbox at the end of the statement but will not release mytxtbox as well.

What I do in this code: I create an object mytxtbox which will remain on the mainform (Form1). I also create an event which will, on validated mytxtbox, will raise. In the event I'm using the using statement to create a TextBox (myeventtxtbox) which is exactly the same that mytextbox (property and so on) but IS NOT mytextbox. SO WHY mytextbox is released while getting out of the using statement? only myeventtxtbox (the copy) should be released.

I really miss something in here, could you help me?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
JudgeDreed
  • 143
  • 1
  • 13

4 Answers4

3

They are reference types. So, the myeventtxtbox doesn't copy anything. Both mytextbox and myeventtxtbox are pointing to the same object and the only difference between them is the name of the reference. Based on this:

Reference Types are used by a reference which holds a reference (address) to the object but not the object itself. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn't copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value. Reference Type variables are stored in a different area of memory called the heap. This means that when a reference type variable is no longer used, it can be marked for garbage collection.

The following posts could be also interesting:

What is the difference between a reference type and value type in c#?

Shallow copy or Deep copy?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
3

I think the problem is that youre assuming this:

TextBox myeventtxtbox = (TextBox)sender

Is making a copy of your text box, so you'll somehow have two text boxes in memory. It doesn't, it simply creates another variable reference to the same text box. sender is also a variable reference to the text box on the form, and so is your textbox Inside the using statement you thus have 3 different variables all pointing to the same textbox

It's critical to appreciate that no matter how many different variables you have referring to it, there's still only one text box. If you'd appreciate an analogy, if the text box were a TV, establishing another variable reference to it is like buying another remote. You now have 3 remotes and any one of them can turn the TV off. If the remote was some intelligent type thing that showed the TV status then all 3 remotes would report the TV being off (disposed) after any one of them turned it off. You can't turn up the volume using remote 2 after turning the TV off with remote 1

Back to the c#

Ultimately calling .Dispose() on any of the variable references to the text box, disposes the text box itself, not the reference to it. The idea of a using statement is that it disposes the object instance that was set up in the round brackets. Calling dispose on any of your names variable references to your textbox (mytextbox, myeventtextbox, sender are all references to it) will dispose the text box itself so when the using statement ends and myeventtextbox.Dispose() is called, all other variable references are now associated with a disposed textbox

We don't need to micro manage variables in c# like we might have to in other languages. Variable references are cleaned up when they go out of scope (when execution leaves the curly braces block the variable was declared in). When there are no more surviving references to an in-memory object, the object will be removed from memory

In this case, sender and myeventtextbox will cease to exist as references to the text box when the event handler finishes executing. The text box will still survive because mytextbox still refers to it. Also the form's .Controls collection refers to it. It you removed the control from .Controls and mytextbox was set = to null, there would be no more surviving references to the text box and it would be removed from memory

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
1

To understand this problem, you first have to understand how objects (reference types) in C# are passed on.

As their name suggests, when you pass on a reference type, the only information you "copy" is its reference (which is basically a memory address in your RAM).

So both textbox variables of yours point to the same exact object in your memory.

Therefore when the using statement ends, your textbox in your GUI is released.

cmos
  • 482
  • 4
  • 14
1

Here mytextbox and myeventtxtbox pointing to the same object because they are reference types.

What you need is to clone the control. I suppose this link will help you :

Clone a control

During clone, you actually copy all props of old control to your new control.

This has prepared a beautiful extension method you can use for all of you controls including TextBox

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
M.Armoun
  • 1,075
  • 3
  • 12
  • 38