Question

I am having issues with the following code.

class AccountDB
{
    public void DumpCustomers(TextWriter textWriter)
    {
        foreach (Customer c in _Customers)
        {
            textWriter.WriteLine("\t{0}", c);
            foreach (Account a in c.Accounts)
            {
                textWriter.WriteLine("\t\t{0}", a.AccountType);
                textWriter.WriteLine("\t\t\t{0,-20} : {1,13}", "Opening Balance", a.OpeningBalanceString);
                foreach (ITransaction t in a.Transactions)
                    textWriter.WriteLine("\t\t\t{0,-20} : {1,13}", t.Description, t);
                textWriter.WriteLine("\t\t\t{0,-20} : {1,13}", "Closing Balance", a);
            }
        }
    }
}


class SavingsAccount : Account
{
    public string AccountType
    {
        get { return "Savings Account"; }
    }
}

class CreditAccount : Account
{
    public string AccountType
    {
        get { return "Credit Account"; }
    }
}

class Customer
{
    private string _Name;
    private List<Account> _Accounts = new List<Account>();

    public string Name
    {
        get { return _Name; }
    }

    public ReadOnlyCollection<Account> Accounts
    {
        get { return _Accounts.AsReadOnly(); }
    }

    public Customer(string Name)
    {
        _Name = Name;
    }

    public void AddAccount(Account account)
    {
        if (!_Accounts.Contains(account))
        {
            _Accounts.Add(account);
            account.RecordCustomer(this);
        }
    }

    public override string ToString()
    {
        return _Name;
    }
}

When the compiler attempts to access the AccountType string, I get the following error:

'Example_Namespace.Account' does not contain a definition for 'AccountType' and no extension method 'AccountType' accepting a first argument of type 'Example_Namespace.Account' could be found (are you missing a using directive or an assembly reference?)

I understand that something must be added to the Account class to allow the code to run (probably polymorphic), but I am unsure how to proceed further. I would greatly appreciate any help. Thanks :)

Was it helpful?

Solution

You should add AccountType property to Account class. There are two ways:

Making Account class abstract:

abstract class Account
{
    public abstract string AccountType { get; }
}

class SavingsAccount : Account
{
    public override string AccountType
    {
        get { return "Savings Account"; }
    }
}

class CreditAccount : Account
{
    public override string AccountType
    {
        get { return "Credit Account"; }
    }
}

Or making AccountType property virtual:

class Account
{
    public virtual string AccountType { get { return string.Empty; } }
}

class SavingsAccount : Account
{
    public override string AccountType
    {
        get { return "Savings Account"; }
    }
}

class CreditAccount : Account
{
    public override string AccountType
    {
        get { return "Credit Account"; }
    }
}

This would make sense if Account should be instantiated on its own and can provide a valid implementation for AccountType property. Otherwise you should go with the abstract class.

OTHER TIPS

Well, you wrote:

foreach (Account a in c.Accounts)
{
 textWriter.WriteLine("\t\t{0}", a.AccountType);
 ...
}

The problem is that you have defined the AccountType property in SavingsAccount and CreditAccount, the classes inherited from the Account class.

The error happens when a in c.Accounts is not a SavingsAccount or CreditAccount.

The solution would be adding the (virtual) AccountType property to the Account class, and overriding it in the child classes.

here:

foreach (Account a in c.Accounts)

You are making a of the account type.

here you are saying that saving account implements account.

class SavingsAccount : Account
{
    public string AccountType
    {
        get { return "Savings Account"; }
    }
}

SavingAccount is where the AccountType exists not in Account. Your base class of Account needs be like this.

public abstract class Account
{
    public abstract string AccountType { get; }
}

and your saving account needs to be this:

public class SavingAccount : Account
{
    public override string AccountType
    {
        get { return "Savings Account"; }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top