user6817211
user6817211

Reputation:

C#: Use of references

I have a form (form1) with a button (Button1) on.

Why does example 1 change text properties on Button1 to "CHANGED" on form1, but example 2 does not? Why is there a difference?

Example 1:

namespace WindowsFormsApplication35
{
    public partial class Form1 : Form
    {
        Button b1 = new Button();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            b1 = button1;
            b1.Text = "CHANGED";
        }
    }
}

Example 2:

namespace WindowsFormsApplication35
{
    public partial class Form1 : Form
    {
        Button b1 = new Button();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1 = b1;
            b1.Text = "CHANGED";
        }
    }
}

Upvotes: 0

Views: 71

Answers (4)

Sweeper
Sweeper

Reputation: 270995

When you do this:

Button b1 = new Button();

You are only creating an instance of Button and storing it in memory. b1 will not be displayed on the screen until you add it to the controls of the form:

Controls.Add(b1);

In Example 1, you did this:

b1 = button1;

This means that you change the value of b1 to the value stored in button1. I assume this button1 here is something generated by the Windows Forms Designer, so it is added to the screen in the InitializeComponents method.

Before the above line is run, b1 stores a (reference to a) button that doesn't appear on the screen. After the line is run, b1 stores a (reference to a) button that does appear on the screen. This is why you can see the change of text after you set the text of b1.

In Example 2, you did this:

button1 = b1;

You change the value of button1 to b1.

Then this line is run:

b1.Text = "CHANGED";

Since b1 still stores a (reference to a) button that doesn't appear on the screen, you can't see the text of b1 change.

Upvotes: 0

Ryan Lundy
Ryan Lundy

Reputation: 210090

In example 1, after creating a Button and assigning b1 to point to it, you're reassigning b1 to point to the same actual Button as button1. Since the button1 Button is on the form, you can see it change.

In example 2, you're doing the reverse: you're creating a Button which b1 points to, and then you're pointing button1 at that same nebulous button. That button was never added to the form's Controls collection, so you can't see it on the form.

Have a look at the code in the InitializeComponent method (use Go to Definition) to see what happens to button1 that your b1 doesn't have.

Upvotes: 0

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

The button you have on screen has two parts to it, the "object" in memory on the heap and a reference to the memory on the heap which is what the variable button1 holds.

Inside InitializeComponent(); the function this.Controls.Add(button1) will get called. What this will do is it will add another reference to the "object" inside the memory of this.Controls.

When you did b1 = button1; you made the reference stored in the variable b1 point at the object that was created with button1 then when you did b1.Text = "CHANGED"; you where calling .Text on the object that the variables b1, button1, and the reference inside this.Controls all pointed at.

When you did button1 = b1; you made the reference stored in the variable button1 point at the object that was created with b1. Now at this point you have b1 and button1 pointing at the object created with b1 and the reference stored inside this.Controls is still pointing at the object that was created with button1 and is now the only reference pointing at that object. When you did b1.Text = "CHANGED"; you where calling .Text on the object that the variables b1 and button1 pointed at but you did not update the object this.Controls pointed at.

And the final piece of the puzzle is the objects whose references are stored in this.Controls are the things that get rendered on screen. That is why the screen did not update.

Upvotes: 0

user6996876
user6996876

Reputation:

It is because button1 is defined in the designer and so it gets rendered in the auto generated code for the form, while the other button is only instantiated but not associated to any form.

Upvotes: 1

Related Questions