这是我的 Mainform.cs 的 C# 代码
public partial class MainForm : Form
{
private Customer cust;
public MainForm()
{
InitializeComponent();
}
private void buttonDeposit_Click(object sender, EventArgs e)
{
// I believe this is where I am going wrong... but I cannot understand why...
DepositDialog dlg = new DepositDialog(cust.Accounts);
dlg.ShowDialog();
}
private void buttonWithdraw_Click(object sender, EventArgs e)
{
// Here it is again...
WithdrawDialog dlg = new WithdrawDialog(cust.Accounts);
dlg.ShowDialog();
}
}
这是 Customer.cs 的代码:
class Customer
{
private BankAccountCollection accounts;
private TransactionCollection transactionHistory;
public Customer()
{
accounts.Add(new SavingsAccount(true,200));
accounts.Add(new SavingsAccount(true, 1000));
accounts.Add(new LineOfCreditAccount(true, 0));
}
public BankAccountCollection Accounts
{
get { return accounts; }
}
public TransactionCollection TransactionHistory
{
get { return transactionHistory; }
}
}
当我尝试运行程序时,我收到一个 JIT 错误,告诉我对象引用未设置为对象的实例。如何初始化字段:
private Customer cust;
为什么需要初始化?: