[英]C#: Use of references
I have a form (form1) with a button (Button1) on. 我有一个窗体(form1)上有一个按钮(Button1)。
Why does example 1 change text properties on Button1 to "CHANGED" on form1, but example 2 does not? 为什么示例1在Form1上将Button1的文本属性更改为“ CHANGED”,而示例2却没有? Why is there a difference?
为什么有区别?
Example 1: 范例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: 范例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";
}
}
}
这是因为button1是在设计器中定义的,因此将在表单的自动生成代码中呈现它,而另一个按钮仅被实例化,而不与任何表单关联。
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. 屏幕上的按钮有两个部分,一个是堆中内存中的“对象”,另一个是变量
button1
拥有的对堆中内存的引用。
Inside InitializeComponent();
内部
InitializeComponent();
the function this.Controls.Add(button1)
will get called. 函数
this.Controls.Add(button1)
将被调用。 What this will do is it will add another reference to the "object" inside the memory of this.Controls
. 这样做是将在
this.Controls
内存中添加对“对象”的另一个引用。
When you did b1 = button1;
当你做
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";
您将存储在变量
b1
的引用指向了用button1
创建的对象,然后执行了b1.Text = "CHANGED";
you where calling .Text
on the object that the variables b1
, button1
, and the reference inside this.Controls
all pointed at. 您在对象上调用
.Text
,变量b1
, button1
和this.Controls
所有引用均指向该对象。
When you did button1 = b1;
当你做
button1 = b1;
you made the reference stored in the variable button1
point at the object that was created with b1
. 您使存储在变量
button1
的引用指向使用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. 现在,此时
b1
和button1
指向使用b1
创建的对象以及存储在其中的引用。 this.Controls
仍然指向使用button1
创建的对象,并且现在是指向该对象的唯一引用。 When you did b1.Text = "CHANGED";
当您完成
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. 您在变量
b1
和button1
指向的对象上调用.Text
,但没有更新this.Controls
指向的对象。
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. 最后一个难题是对象的引用存储在其中。
this.Controls
是呈现在屏幕上的对象。 That is why the screen did not update. 这就是为什么屏幕没有更新的原因。
In example 1, after creating a Button
and assigning b1
to point to it, you're re assigning b1
to point to the same actual Button
as button1
. 在示例1中,在创建一个
Button
并分配b1
指向它之后,您将重新分配b1
使其指向与button1
相同的实际Button
。 Since the button1
Button is on the form, you can see it change. 由于
button1
按钮位于表单上,因此您可以看到它发生了变化。
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. 在示例2中,您做相反的事情:创建一个
b1
指向的Button
,然后将button1
指向相同的模糊按钮。 That button was never added to the form's Controls collection, so you can't see it on the form. 该按钮从未添加到窗体的Controls集合中,因此您无法在窗体上看到它。
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. 查看
InitializeComponent
方法中的代码(使用“转到定义”),以查看b1
没有的button1
发生了什么。
When you do this: 执行此操作时:
Button b1 = new Button();
You are only creating an instance of Button
and storing it in memory. 您只创建一个
Button
实例并将其存储在内存中。 b1
will not be displayed on the screen until you add it to the controls of the form: 除非将
b1
添加到窗体的控件中,否则它不会显示在屏幕上:
Controls.Add(b1);
In Example 1, you did this: 在示例1中,您这样做:
b1 = button1;
This means that you change the value of b1
to the value stored in button1
. 这意味着您将
b1
的值更改为存储在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. 我假设这里的
button1
是Windows窗体设计器生成的,因此它被添加到屏幕的InitializeComponents
方法中。
Before the above line is run, b1
stores a (reference to a) button that doesn't appear on the screen. 在运行上述行之前,
b1
存储一个未出现在屏幕上的(对a的引用)按钮。 After the line is run, b1
stores a (reference to a) button that does appear on the screen. 运行该行之后,
b1
存储一个确实出现在屏幕上的(对a的引用)按钮。 This is why you can see the change of text after you set the text of b1
. 这就是为什么在设置
b1
的文本后可以看到文本的变化。
In Example 2, you did this: 在示例2中,您这样做:
button1 = b1;
You change the value of button1
to b1
. 您将
button1
的值更改为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. 由于
b1
仍存储屏幕上未出现的(对a的)按钮,因此您看不到b1
的文本更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.