setter c#显示错误,请确保您没有无限循环或无限递归
本文关键字:无限循环 递归 无限 确保 显示 错误 setter | 更新日期: 2024-10-21 12:05:54
这是在表单2中获取用户输入和在表单1中显示数据的主题连接。
这是我在表格2中的代码。
public string UserText
{
get
{
return this.textBox1.Text;
}
set
{
this.UserText = value;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Please enter keyword to search");
}
else
{
//anta data input to form1.
UserText = textBox1.Text;
}
这是我在表格1 中的代码
private void Form1_Load(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
string text = form2.UserText;
}
我希望当我们点击按钮搜索时,它会在我们加载表单时自动显示数据1。
当我跑步时,它在二传手处说:
确保没有无限循环或无限递归。
为什么这么说?我做错了什么?
我也试过这么做。
public string UserText
{
get
{
return this.UserText;
}
set
{
this.UserText = value;
}
}
但它看起来是一样的。
===编辑====现在我尝试使用这个:
public string UserText
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}
我也试过这个:
public string UserText { get; set;}
它没有显示错误,但是也没有加载form1。手术就到此为止了。我做错了什么吗?
UserText
属性的setter中的代码:this.UserText = value;
调用自己。基于getter,我认为你应该让setter像这样:
set
{
this.textBox1.Text = value;
}
如果您使用的是C#4
public string UserText {get;set;}
您必须将Form2放入Form1构造函数
public Form1()
{
this.InitializeComponent();
Form2 form2 = new Form2();
form2.ShowDialog();
string text = form2.UserText;
}
这将在Form1显示之前发生