简体   繁体   English

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

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

I have got a class called Customers with an Arraylist inside to store information about the Customer Accounts; 我有一个叫做带有Arraylist的客户的班级来存储有关客户账户的信息; then inside Accounts class I have an Arraylist to hold the Transactions. 在Accounts类中,我有一个Arraylist来保存Transactions。

My question is, how would I go about saving to the Arraylist found within the Customer Class. 我的问题是,我将如何保存到客户类中找到的Arraylist。 It doesnt seem like I can access it. 它似乎无法访问它。

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

This is my method on a form to add to the Arraylist. 这是我在表单上添加到Arraylist的方法。 It first checks the input is OK, then creates a new Account called temp (Account is the Class name). 它首先检查输入是否正常,然后创建一个名为temp的新帐户(Account是类名)。 Then how do I go about saving this inside of the Arraylist inside the Class Account? 那么如何在课堂账户中将其保存在Arraylist中呢?

Thanks. 谢谢。

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

Then in your code: 然后在你的代码中:

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

Building on Swaff's answer earlier, just make your Accounts ArrayList private and expose the AddAccount functionality: 基于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