Francis Ducharme
Francis Ducharme

Reputation: 4987

Class inheritance & constructors

Hello to all the brilliant minds of StackOverflow!

I am getting familiar with c# class inheritance and multiple constructors but I can't seem to word a question that would allow me to Google it.

Here's what I have:

public class Order: OtherOrder
{

    private OtherOrderManager _om;

    public Order()
    {
        if (_om == null)
            _om = new OtherOrderManager();
    }

    public Order(int orderID)
        : base()
    {

    }
}

So obviously, now I can do something like:

Order order = new Order();
order.Member_From_OtherOrder_Class = "bleh";

But here's what I'm trying to implement in a constructor:

public class Order: OtherOrder
{

private OtherOrderManager _om;

public Order()
{
    if (_om == null)
        _om = new OtherOrderManager();
}

public Order(int orderID)
    : base()
{
   this = (Order)_om.GetOrder(orderID); //Returns an instance of OtherOrder
   //Basically, I want to populate all the members of Order() interited
   //from OtherOrder and fill them with data returned by this call.
}

}

Obviously, "this" is read only so that doesn't even compile. Is there any technical expression that would describe what I'm looking for ? Is it even possible ?

Thanks!

EDIT: I think I'll use reflection to loop through all members and get/set values this way.

Upvotes: 2

Views: 253

Answers (3)

Chris Sinclair
Chris Sinclair

Reputation: 23218

Even though it's a bit vague about what you're trying to achieve, I'm guessing you might want to use something along the lines of using a factory possibly with copy-constructors. Essentially, the copy-constructors provide an easy means to populate data along the inheritance chain with your copies.

Consider the following base class:

public abstract class OrderBase
{
    public int OrderID { get; private set; }
    public string Name { get; protected set; }

    public OrderBase()
    {

    }

    public OrderBase(OrderBase copiedOrder)
    {
        this.OrderID = copiedOrder.OrderID;
        this.Name = copiedOrder.Name;
    }
}

(I left the parameterless constructor in there because I'm guessing it will be needed somewhere)

So an OrderBase can be instantiated by passing another OrderBase instance. Within that constructor, we know which properties to copy over and are compile-time checked.

Now a subclass might be:

public class CustomOrder : OrderBase
{
    public Guid CustomID { get; private set; }

    public CustomOrder()
    {

    }

    public CustomOrder(CustomOrder copiedOrder)
        : base(copiedOrder)
    {
        this.CustomID = copiedOrder.CustomID;
    }
}

Here, the CustomOrder only copies its own property that is declared on it and passes the rest of the copying responsibility to the base class. Add one more class to the chain:

public class ValidatableCustomOrder : CustomOrder
{
    public bool IsValid { get; private set; }

    public ValidatableCustomOrder()
    {

    }

    public ValidatableCustomOrder(ValidatableCustomOrder copiedOrder)
        : base(copiedOrder)
    {
        this.IsValid = copiedOrder.IsValid;
    }
}

And you can see how it can nicely manage each property set per subclass without any one class knowing much about the other. Your factory in turn might look something like:

public static class ValidatableCustomOrderLoader
{
    public static ValidatableCustomOrder Get(int orderID)
    {
        ValidatableCustomOrder loadedOrder = LoadOrderFromSomewhere(orderID);
        ValidatableCustomOrder copiedOrder = new ValidatableCustomOrder(loadedOrder);
        return copiedOrder
    }

    private ValidatableCustomOrder LoadOrderFromSomewhere(int orderID)
    {
        //get your order somehow
    }
}

So it (somehow) loads the data (perhaps from a database), copies it to a new instance which will chain through all the copy-constructors. Your calling code would look like:

int orderID = 10;
ValidatableCustomOrder order = ValidatableCustomOrderLoader.Get(orderID);

Anyhow, I can't say if this will specifically help you since your question/code seems a bit off-the-wall and vague, but hopefully it will help give you some ideas.

Upvotes: 5

Tim S.
Tim S.

Reputation: 56576

Two approaches come to mind: manually copy the properties:

public Order(int orderID)
    : base()
{
   var other = (Order)_om.GetOrder(orderID);
   this.SomeProperty = other.SomeProperty; //repeat for each property/field that should be copied
}

Or use a static or factory method instead of constructors, e.g.:

public static Order GetOrder(int orderId)
{
    return (Order)_om.GetOrder(orderID);
}

Upvotes: 1

BlackHatSamurai
BlackHatSamurai

Reputation: 23503

Try:

this._om = (Order)_om.GetOrder(orderID);

Hope that helps.

Upvotes: 0

Related Questions