C# "Rename" Property in Derived Class

bopapa_1979 picture bopapa_1979 · Sep 7, 2012 · Viewed 9.2k times · Source

When you read this you'll be awfully tempted to give advice like "this is a bad idea for the following reason..."

Bear with me. I know there are other ways to approach this. This question should be considered trivia.

Lets say you have a class "Transaction" that has properties common to all transactions such as Invoice, Purchase Order, and Sales Receipt.

Let's take the simple example of Transaction "Amount", which is the most important monetary amount for a given transaction.

public class Transaction
{
    public double Amount { get; set; }

    public TxnTypeEnum TransactionType { get; set; }
}

This Amount may have a more specific name in a derived type... at least in the real world. For example, the following values are all actually the same thing:

  • Transaction - Amount
  • Invoice - Subtotal
  • PurchaseOrder - Total
  • Sales Receipt - Amount

So now I want a derived class "Invoice" that has a Subtotal rather than the generically-named Amount. Ideally both of the following would be true:

  1. In an instance of Transaction, the Amount property would be visible.
  2. In an instance of Invoice, the Amount property would be hidden, but the Subtotal property would refer to it internally.

Invoice looks like this:

public class Invoice : Transaction
{
    new private double? Amount
    {
        get
        {
            return base.Amount;
        }
        set
        {
            base.Amount = value;
        }
    }

    // This property should hide the generic property "Amount" on Transaction
    public double? SubTotal
    {
        get
        {
            return Amount;
        }
        set
        {
            Amount = value;
        }
    }

    public double RemainingBalance { get; set; }
}

But of course Transaction.Amount is still visible on any instance of Invoice.

Thanks for taking a look!

Answer

bopapa_1979 picture bopapa_1979 · Sep 8, 2012

Thanks for all the help.

OK, of course you cannot "hide" public properties on the base class when the derived class IS a base instance. Somewhere deep in my brain I already knew that. Doh!

I wound up getting the syntactic sugar to behave the way I wanted for the consumer by using a third class called TransactionBase. This class is abstract and contains the shared, non-aliased stuff that exists for all transactions like currency, exchange rate, created/modified date and time, transaction date, etc... in addition to aliased stuff like Amount.

Here, I just show the Amount property in question:

public abstract class TransactionBase
{
    protected virtual double Amount { get; set; }
}

Then Transaction looks like this:

public class Transaction : TransactionBase
{
    public new double Amount 
    { 
        get
        {
            return base.Amount;
        }
        set
        {
            base.Amount = value;
        }
    }
}

And Invoice:

public class Invoice : TransactionBase
{
    public double SubTotal
    {
        get
        {
            return Amount;
        }
        set
        {
            Amount = value;
        }
    }
}

And access works the way I wanted:

var transaction = new Transaction();

// This line works fine:
var transactionAmount = transaction.Amount;



var invoice = new Invoice();

// This line works fine:
var invoiceSubtotal = invoice.SubTotal;

// This line won't compile.
// Error: TransactionBase.Amount is inaccessible due to its protection level.
var invoiceAmount = invoice.Amount;

So the answer to my original question was, "no" you cannot hide public inherited members. The above solution fakes it with accessing the protected member directly in the derived types, but it still sort of sucks. Too much typing.

Of course, now that I fiddled and piddled with all that, I'm seeing that a better solution throws out the protected members altogether and saves me some typing. By the way, YES I am embarrassed that I didn't jump immediately to this solution.

EDIT: Actually, the first appraoch in my answer might be better. With the 2nd one, I'd lose the "Amount" or "Subtotal" when casting from a Transaction to an Invoice.

public abstract class TransactionBase
{
    // There are some shared properties here.
}

public class Transaction : TransactionBase
{
    public double Amount { get; set; }
}

public class Invoice : TransactionBase
{
    public double SubTotal { get; set; }
}