可以放置"view = new Person"
在按钮事件处理程序中吗?如果我不这样做,并且我将它放在 Form1 构造函数中,那么只会添加我的最后一个值。如果我想声明一个新实例然后将它添加到我的 Arraylist,这是正确的方法吗?
private ArrayList store;
public Form1()
{
InitializeComponent();
store = new ArrayList();
}
private void Form1_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
//Is it okay to declare a new instance of the Person class
// with each button push?
Person view = new Person();
view.firstname = txtFirstName.Text;
view.lastname = txtLastName.Text;
store.Add(view);
txtFirstName.Clear();
txtLastName.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
foreach (Person display in store)
{
MessageBox.Show(display.ToString());
}
}