在form3中写的代码,定义了几个公有属性
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public static string s1;
public static string s2;
public static string s3;
public static string s4;
public static string s5;
public static string s6;
}
form5里的代码是这个样子的
private void Form5_Load(object sender, EventArgs e)
{
Form form3 = new Form3();
textBox1.Text = form3.s1;
}
但是在form5中引用的时候显示 错误 CS1061 “Form”未包含“s1”的定义,并且找不到可接受第一个“Form”类型参数的可访问扩展方法“s1”(是否缺少 using 指令或程序集引用?)
想要在form5中引用这几个值,应该怎么办?
请注意你在Form3
中声明的s1
...这些变量,均是用static
修饰的,所以不能以实例对象的方式访问。
而应该以类的静态方法方式访问,如下:
private void Form5_Load(object sender, EventArgs e)
{
textBox1.Text = Form3.s1;
}
Form3 form3 = new Form3();
你把form3装进Form基类里面,当然找不到Form3里定义的属性了