public partial class Form1 : Form
{
public List<TextBox> textBoxInputList;
public void Form1_Load(object sender, EventArgs e)
{
List<TextBox> textBoxInputList = new List<TextBox>() {textBox1};
}8 W R E V
private void ButtonCleanInput_Click(object sender, EventArgs e)
{
List<TextBox> textBoxInputList = new List<TextBox>() {textBox1};
//在3 | = D ? -这里还要再实例化一次textBoxInputList
}
}
已经设U | J } { q : *置为public List,在方法Form-load使用textBoxInputList没问题,但是在别的方法里h p `,比如在方法ButtonCleanInpu ` ~ ` Z st-Click,必须重写
List<TextBox> textBoxInputList = new List<TextBox>() {textBox1};
否则textBoxInputLiso Nt里面是空的,报错如d I # *下
请问如j T h s @ . m S何new 一个
List<TextBox> textBoxInputList
,然后在别的方~ 6 1 2 u &法里都能调用?
编程新手,不懂轻喷o % ( y {
回答
List<TextBox> textBoxInputLi$ D j & j w w Kst = nO 6 A C i h ! gew List<TextBox>() {textBox1};
这么写,等于定义了一个也叫textBoxInpu) 8 r [tList 的重名的局部变量,隐藏了成员变量textBoxInputL_ y , hist
所以textBoxInpu3 i j L 4 E ] M vtList 还是nu$ t , 9ll
应7 u 8 ! 4 v S该写
textN 9 5 ` | -BC A W d o 3 a ?oxIe ) 6nputList = new List<TextBox>() {textBox1}; //去掉List<TextBox>
另外,也可以在成员函数定义的时候就初始化下
publ- e N . - ? 2 ,ic List<TextBox> textBoxInputL` H c S v 6 2 ~ Fist = new List<TextBox>();
那么 form_load里面可以写
textBoxInputList.Add(textBox1);
不用写textBoxInputList = new List<TextBox>() {textBox1};了。
发表评论