0

这是我的 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;

为什么需要初始化?

4

7 回答 7

2

问题在于,在构造函数中,您使用的是accounts 对象,而没有首先使用'new' 关键字创建对象。在使用 .Add 方法之前,您必须按如下方式初始化对象。

private BankAccountCollection accounts = new BankAccountCollection();
private TransactionCollection transactionHistory = new TransactionCollection();

public Customer()
{
    accounts.Add(new SavingsAccount(true,200));
    accounts.Add(new SavingsAccount(true, 1000));
    accounts.Add(new LineOfCreditAccount(true, 0));
}
于 2011-11-16T10:38:19.813 回答
1

“客户的客户”;只告诉编译器 cust 可以保存什么类型的变量。它实际上并没有为客户对象分配任何内存......这就是'new'关键字的作用。不要将 cust 视为一个对象,而是将其视为指向客户对象的指针,如果您愿意,您可以随时更改为指向不同的客户对象。

于 2011-11-16T10:21:11.687 回答
1
why does it need to be initialized?

non-static因为,如果不创建实例,您将无法访问成员。

在访问其实例成员之前,您需要Customer使用new关键字实例化该类。

public partial class MainForm : Form
{
    private Customer cust;

    public MainForm()
    {
        InitializeComponent();
        cust = new Customer();
     }
于 2011-11-16T10:28:06.090 回答
1

它不像创建那样被初始化。

 private Customer cust; // tells the compiler cust will be a Customer

 cust = new Customer(); // tells the compiler to create(and initialise) a Customer and point cust at it.

除非您这样做,否则 cust 将是空的,并且任何用它来做某事的客户都将是一个错误。

另一种思考变量 cust 的方法是。

  cust = FindCustomerInCountry("France");

如果您没有法国客户 cust 将指向“无”,即 null。

继续前进,你会到达那里。

于 2011-11-16T10:41:08.167 回答
1

您可以在表单构造函数中对其进行初始化

public MainForm()
{
    InitializeComponent();
    cust = new Customer();
    // here eventually some intialization code...

或直接通过声明。

private Customer cust = new Customer() 
      {Accounts = new BankAccount[] 
          { new SavingsAccount(true,200), new SavingsAccount(true,200), 
            new SavingsAccount(true,200)} };
于 2011-11-16T10:17:21.003 回答
1

你的客户是空的。

初始化它:

private Customer cust = new Customer();

或者

在这条线之前

DepositDialog dlg = new DepositDialog(cust.Accounts);
于 2011-11-16T10:19:23.700 回答
0
public MainForm()
{
    InitializeComponent();
    cust = new Customer();
}
于 2011-11-16T10:17:22.093 回答