正在初始化整个winform要使用的字段..(对象引用未设置为对象的实例)

本文关键字:对象引用 设置 实例 对象 字段 初始化 winform | 更新日期: 2023-09-27 18:14:41

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

以及为什么需要对其进行初始化

正在初始化整个winform要使用的字段..(对象引用未设置为对象的实例)

问题是,在构造函数中,您使用的是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));
}

您可以在表单构造函数中初始化它

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)} };

您的客户机为空。

初始化它:

private Customer cust = new Customer();

在这条线路之前

DepositDialog dlg = new DepositDialog(cust.Accounts);

"客户cust;"仅告诉编译器cust可以容纳什么类型的变量。它实际上并没有为客户对象分配任何内存。。。这就是"new"关键字的作用。不要把cust看作一个对象,而是一个指向客户对象的指针,如果你愿意,你可以在任何时候更改为指向不同的客户对象。

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();
     }

与其说它是初始化的,不如说它是创建的。

 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将为null,任何试图用它为客户做某事的行为都将是一个错误。

另一种思考可变客户是什么的方法是

  cust = FindCustomerInCountry("France");

如果你没有法国客户,cust会指向"nothing",即null。

继续走,你会到达那里的。

public MainForm()
{
    InitializeComponent();
    cust = new Customer();
}