3

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..
}
user3378165
  • 6,546
  • 17
  • 62
  • 101
  • 1
    post definition for class `Loan` – Rahul Jan 07 '19 at 07:11
  • 2
    I think you may have misunderstood what "inheritance" means. It means that the derived class (`LoanWithClient`) takes on the features (methods, properties, etc.) of its parent (`Loan`). If neither `LoanWithClient` nor `Loan` have a property `Loan`, then you won't be able to use it when constructing an instance. If `Loan` has an `Amount` property, you can use this directly as you do with `Client`. – ProgrammingLlama Jan 07 '19 at 07:13
  • 1
    Create a constructor and call the base-class constructor from it. See e.g. https://stackoverflow.com/questions/12051/calling-the-base-constructor-in-c-sharp – DodgyCodeException Jan 07 '19 at 07:22
  • Also, your object model looks a bit suspicious. Is address really a property of a loan? – DodgyCodeException Jan 07 '19 at 07:23
  • Perhaps you're looking for [the decorator pattern?](https://stackoverflow.com/questions/2707401/understand-the-decorator-pattern-with-a-real-world-example) (see also: [decorator pattern Wikipedia page](https://en.wikipedia.org/wiki/Decorator_pattern)) – ProgrammingLlama Jan 07 '19 at 07:26
  • 1
    Don't mix up *is-a* and *has-a*. `LoanWithClient` *is* a `Loan`. It doesn't *have* a `Loan`. – Damien_The_Unbeliever Jan 07 '19 at 07:27

2 Answers2

5

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
};
Ashique
  • 345
  • 1
  • 8
4

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
};
shingo
  • 18,436
  • 5
  • 23
  • 42