我有一个叫做带有Arraylist的客户的班级来存储有关客户账户的信息;然后在Accounts类中我有一个Arraylist来持有Transactions。
我的问题是,如何保存到客户类中找到的Arraylist。我似乎无法访问它。
if (allInputOK)
{
//create Account
Account temp = new Account(tempAccSortCode, tempAccNumber, tempAccNickName, tempAccDate, tempAccCurBal, tempAccOverDraft, tempNumTrans);
//add to array
//Need to add here.
//finish up
MessageBox.Show("Success Account added ");
resetForm();
}
这是我在表单上添加到Arraylist的方法。它首先检查输入是否正常,然后创建一个名为temp的新帐户(Account是类名)。那么如何在班级帐户内部将其保存在Arraylist中呢?
感谢。
答案 0 :(得分:1)
public class Account
{
}
public class Customer
{
public ArrayList Accounts
{
get;
private set;
}
public Customer()
{
Accounts = new ArrayList();
}
public void AddAccount(Account account)
{
// if account is valid add it to the local collection
Accounts.Add(account);
}
}
然后在你的代码中:
if (allInputOK)
{
//create Account
Account temp = new Account(tempAccSortCode, tempAccNumber, tempAccNickName, tempAccDate, tempAccCurBal, tempAccOverDraft, tempNumTrans);
//add to array
_customer.AddAccount(temp);
//finish up
MessageBox.Show("Success Account added ");
resetForm();
}
答案 1 :(得分:1)
早在Swaff的回答基础上,只需将您的Accounts
ArrayList
设为私有,并公开AddAccount功能:
public class Customer {
private ArrayList _accounts = new ArrayList();
...
public void AddAccount(Account theAccount){
//do some validation...if OK, then add to ArrayList...
_accounts.Add(theAccount);
}
//you'll also need facade methods to retrieve accounts
}