0

I have got a class called Customers with an Arraylist inside to store information about the Customer Accounts; then inside Accounts class I have an Arraylist to hold the Transactions.

My question is, how would I go about saving to the Arraylist found within the Customer Class. 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. It first checks the input is OK, then creates a new Account called temp (Account is the Class name). Then how do I go about saving this inside of the Arraylist inside the Class Account?

Thanks.

2
  • 1
    Assuming I'm following what you're saying... With your given structure, you'd need to expose some way to add an account to your customer, which would perform the insertion into the array list and then call this from the code you've supplied. Commented Mar 21, 2011 at 13:37
  • I guess the question is why do you want a method to add an Account to your ArrayList? Why not expose your ArrayList as Swaff describes below and just use the built-in ArrayList methods (like Add())? Commented Mar 21, 2011 at 14:11

2 Answers 2

1
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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, your code just prevents the user of the class from replacing the Accounts ArrayList wholesale. They can still do this: _customer.Accounts.Add(newCustomer). You probably want to make the Accounts property private or internal to the Customer class and just expose a facade around it for specific actions.
Hmm, following you code as though i think i should be: I have placed the top snippet into the Customer Class, and the second snippet into the Form, but i'm still a little confused over the _customer.AddAccount(temp) line. The compiler is also throwing a message saying _customer not in context. I tried replacing that with Customer, but that through and error saying An object reference is required. Could you explain the code a little please?
@user667430 The line _customer.AddAccount(temp) assumes that somewhere in your form you have created an object of type Customer already that you wish to add accounts to. If you have not then you will need to instantiate one before you try to use it.
1

Building on Swaff's answer earlier, just make your Accounts ArrayList private and expose the AddAccount functionality:

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
}

1 Comment

@david-hoerster Nice encapsulation! Good addition to my suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.