【发布时间】:2015-04-06 10:54:54
【问题描述】:
我正在尝试使用属性将两个集合从一个表单传输到另一个表单。 但是由于某种原因,我在 form1 中看不到来自 form2 的属性。我得到的错误信息是
System.Windows.Forms.Form 不包含 _col1 和 没有扩展方法 _col1 接受类型的第一个参数 System.windows.Forms.Form....
这是来自 Form1 的代码
public partial class Form1 : Form
{
private Collection<string> col1;
private Collection<string> col2;
private void btn1_Click(object sender, EventArgs e)
{
Form frm1 = new Form2();
//fill collections with some kind of data
frm1._col1 = _col1;
frm1._col2 = _col2;
frm1.Show();
}
public Collection<string> _col2
{
get { return col2; }
}
public Collection<string> _col1
{
get { return col1; }
}
}
这是来自 Form2 的代码
public partial class Form2 : Form
{
private Collection<string> col1;
private Collection<string> col2;
public Form2()
{
InitializeComponent();
}
public Collection<string> _col1
{
get { return col1; }
set { col1 = value; }
}
public Collection<string> _col2
{
get { return col2; }
set { col2 = value; }
}
}
根据我已阅读的文章,一切都应该工作 - 但是我无法从 Form1 访问 Form2 属性。
我错过了什么??
【问题讨论】:
-
虽然您正在实例化 Form2,但您的本地引用是基址
Form。将btn1_Click中的行更改为Form2 frm1 = new Form2();(并考虑为您的类和局部变量赋予更有意义的名称) -
并不是说它会有所作为,但通常私有成员是以下划线开头的成员
-
感谢 StuartLC - 这就是解决方案。
-
感谢 CoderDennis 的建议 - 我将更改名称以符合通用标准。我对编程很陌生,还有很多东西要学……
-
@Stefan:如果您的问题得到解决,请将解释如何解决问题的答案之一标记为已接受。这将向未来的访问者发出问题已得到解答的信号,以及在哪个答案中找到可行的解决方案。