繁体   English   中英

C#将数据从输入存储到Class内的arraylist

[英]C# storing data from input to arraylist inside Class

我有一个叫做带有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中呢?

谢谢。

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

基于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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM