2

I am new to Asp .net C#. i have question about objects and inheritance. if i have parent class (Base-Table) that have 2 child classes (Credit-Card-Table , Bank-Account-Table) i have fun. in another class that take an object from the base-table class. my problem is i want to know if the Base-table is Credit-card or Bank-account ?!

class BaseTable
{
    string date;
    public string Date
    {
        get { return date; }
        set { date = value; }
    }

    string description;
    public string Description
    {
        get { return description; }
        set { description = value; }
    }

}



class CreditCardTable:BaseTable
{
    string Amount;
    public string amount
    {
        get { return Amount; }
        set { Amount = value; }
    }

    string Type;
    public string type
    {
        get { return Type; }
        set { Type = value; }
    }

}



class BankAccountTable:BaseTable
{
    string Refr;

    public string Ref
    {
        get { return Refr; }
        set { Refr = value; }
    }
    string debit;

    public string Debit
    {
        get { return debit; }
        set { debit = value; }
    }

    string credit;

    public string Credit
    {
        get { return credit; }
        set { credit = value; }
    }

}
3
  • 1
    More often than not something like this is an indication of an error in the design. Maybe BaseTable should be abstract? Maybe the other code needs a child instance and not a base instance? What specific reason do you have to need to know the type of the instance? Commented Jun 26, 2013 at 11:16
  • 1
    Why do you need to know what type your BaseTable is? Usually this means you've gone wrong in your design. The usual way of approaching this would be to make a virtual/abstract method in BaseTable that is overridden in the derived classes to implement the type-specific behaviour. That way, anything using a BaseTable reference can call the virtual method and get different behaviour depending on the actual type of the BaseTable reference. Commented Jun 26, 2013 at 11:17
  • its a code to create a pdf there is 2 types of pdfs credit card pdf and bank account pdf so in the business i want to check if the obj is credit card or bank account so i can choose which one to create Commented Jun 26, 2013 at 11:29

1 Answer 1

2

3 options:

  1. use is, as or GetType() to explicitly check the type of an instance you have been given, to test it against some known types

    if(obj is CreditCardTable) {...} else ...
    
  2. add a virtual or abstract method to the base-type, and use that instead of ever having to worry about which it is (since it will automatically invoke the most derived override)

    obj.SomeMethod();
    
  3. add a discriminator - perhaps a virtual enum property to the BaseTable which all derived types return a different value from, and switch on that discriminator:

    switch(obj.Type) { ... }
    
Sign up to request clarification or add additional context in comments.

2 Comments

Number two!!!!! Switching on type (effectively options 1&3) suggests a design problem to me.
@spender depends on the scenario; that would be my preference too, but in some cases separation-of-concerns means that the type itself shouldn't know this "whatever"; for example, if the entity is a POCO, and the "whatever" is "an ORM persisting it", then I would accept the ORM using .GetType() and comparing that to the expected types; it isn't the job of the POCO to persist. Likewise with a serializer; so "number one" definitely has some utility

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.