Access inherited member

Viewed 325

I have this LoanWithClient Model that inherits from Loan:

public class LoanWithClient : Loan
{
    public Client Client { get; set; }
}

How can I access the entire inherited Loan object without having to explicitly write its properties?

LoanWithClient does not contain a definition for Loan

return new LoanWithClient
{
     **Loan** = loan, //The Loan is erroring: LoanWithClient does not contain a definition for Loan
     Client = client
};

Class Loan:

public class Loan
{
    public int ID { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    //etc..
}
2 Answers

The class LoanWithClient inherits from Loan. Which means that the child class have all the properties of parent class. But this doesn't mean that the child class contains a parent class as a property. You can write the class like this-

public class Loan
{
    public int ID { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    //etc..
}

public class LoanWithClient
{
    public Loan Loan { get; set; }
    public Client Client { get; set; }
}

return new LoanWithClient
{
     Loan = loan,
     Client = client
};

If you want to keep your class architecture, you can return like the below way-

return new LoanWithClient
{
     ID = loan.ID,
     Address = loan.Address,
     City = loan.City,
     //etc..
     Client = client
};

You want to

Access inherited member

Loan is not a member, it's the parent. Access Loan's members like this:

return new LoanWithClient
{
     ID = loan.ID,
     Address = loan.Address,
     City = loan.City,
     //etc...
     Client = client
};
Related