7

I have a BankAccount class. FixedBankAccount and SavingsBankAccount are derived from it.
I need to throw an exception if the recieved object is not a derived object. I have the following code.

IEnumerable<DBML_Project.BankAccount> accounts = AccountRepository.GetAllAccountsForUser(userId);
foreach (DBML_Project.BankAccount acc in accounts)
{
    string typeResult = Convert.ToString(acc.GetType());
    string baseValue = Convert.ToString(typeof(DBML_Project.BankAccount));

    if (String.Equals(typeResult, baseValue))
    {
        throw new Exception("Not correct derived type");
    }
}

namespace DBML_Project
{

public  partial class BankAccount
{
    // Define the domain behaviors
    public virtual void Freeze()
    {
        // Do nothing
    }
}

public class FixedBankAccount : BankAccount
{
    public override void Freeze()
    {
        this.Status = "FrozenFA";
    }
}

public class SavingsBankAccount : BankAccount
{
    public override void Freeze()
    {
        this.Status = "FrozenSB";
    }
}

} // namespace DBML_Project

Is there any better code than this?

Eitan T
  • 32,660
  • 14
  • 72
  • 109
LCJ
  • 22,196
  • 67
  • 260
  • 418

5 Answers5

12

You should be using Type.IsAssignableFrom

if (acc.GetType().IsAssignableFrom(typeof(BankAccount)))
    // base class
else
    // derived
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
5

Declare BankAccount class as Abstract.

bhuvin
  • 1,382
  • 1
  • 11
  • 28
3

I would define an interface (something like IAccettableBankAccount, but you know your domain, so you should be able to find a better name) and have FixedBankAccount and SavingsBankAccount implement it. Then your test would simply be:

if (!acc is IAccettableBankAccount)
{
     throw new Exception("Not correct derived type");
}
Francesco Baruchelli
  • 7,320
  • 2
  • 32
  • 40
  • Thanks for the idea. Is it better than my current implementation? – LCJ Jul 03 '12 at 08:54
  • 1
    It depends. For sure it gives you more control, i.e. in your implementation every subclass of BankAccount (even future classes) will always be OK, with the interface you could have classes which are derived from BankAccount, but can throw your exception. – Francesco Baruchelli Jul 03 '12 at 08:58
2

Use Type.IsSubclassOf Method. For more info check this

foreach (DBML_Project.BankAccount acc in accounts)
{
    if (!acc.GetType().IsSubclassOf(typeof(DBML_Project.BankAccount))
    {
        throw new Exception("Not correct derived type");
    }
}
AksharRoop
  • 2,263
  • 1
  • 19
  • 29
0

You can just check the types directly:

    var accounts = AccountRepository.GetAllAccountsForUser(userId);
    if (accounts.Any(acc => acc.GetType() == typeof(DBML_Project.BankAccount))) {
        throw new Exception("Not correct derived type");
    }

Declaring the class as abstract might help, too, but i don't know if that's an option in your case.

Botz3000
  • 39,020
  • 8
  • 103
  • 127