C# Initialize Subclass based on Parent object

Posted by ctrlShiftBryan on Stack Overflow See other posts from Stack Overflow or by ctrlShiftBryan
Published on 2010-05-21T19:16:36Z Indexed on 2010/05/21 19:20 UTC
Read the original article Hit count: 318

Filed under:
|
|

So basically I have this

public class Ticket{
    public TicketNumber {get; set;}
    ..a bunch more properties...
}

I want to add some properties using a subclass like this using subsumption instead of composition.

public class TicketViewModel(Ticket ticket){
    //set each property from value of Ticket passed in
    this.TicketNumber = ticket.TicketNumber;
    ...a bunch more lines of code..

    //additional VM properties
    public SelectList TicketTypes {get; private set;}
}

How do I instantiate the properties without having to write all the lines like this

this.TicketNumber = ticket.TicketNumber;

Is there some kind of shortcut? Something like in the subclass constructor?

this = ticket; 

Obviously this doesn't work but is their some way so I don't have to modify my subclass if addng/removing a property to the parent class? Or something?

© Stack Overflow or respective owner

Related posts about c#

Related posts about initialization