Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange Inheritance Issue with C# classes
    primarykey
    data
    text
    <pre><code>public class DTOa { public int Id { get; set;} public string FirstName { get; set;} } public class DTOb: DTOa { public string Email { get; set;} public string Password { get; set; } } public class a { protected DTOa _DTO = new DTOa(); public int Id { get { return _DTO.Id; } set { _DTO.Id = value; } } public string FirstName { get { return _DTO.FirstName; } set { _DTO.FirstName = value; } } public DTOa ToValueObject() { return _DTO; } } public class b: a { protected DTOb _DTO = new DTOb(); public string Email { get { return _DTO.Email; } set { _DTO.Email = value; } } public string Password { get { return _DTO.Password; } set { _DTO.Password = value; } } public DTOb ToValueObject() { return _DTO; } } </code></pre> <p>now let's execute the following code</p> <pre><code>public function test() { var a = new a(); var b = new b(); b.Id = 100; b.FirstName = "Jim"; b.Email = "email@email.com"; b.Password = "test"; Console.WriteLine(b.ToValueObject().Dump()); } </code></pre> <p>the problem is that </p> <ol> <li><p>I expect b.ToValueObject have all properties set, but in reality only get properties from DTOb class (so I FirstName and Id properties are NULL, however I set the explicitly)</p> <pre><code>dump: { Email: email@email.com, Password: test, Id: 0 } </code></pre></li> </ol> <p>Any ideas why ID is not set and FirstName is not set? DTOb is inherited from DTOa and thus "Should" include all the properties from DTOa. It's working on the code level, so if I write <code>console.WriteLine(b.Firstname)</code> - I'll get the value correct, but when I call <code>ToValueObject()</code> method - it got deleted.</p> <hr> <p>OKay here is working example:</p> <pre><code>public class DTOa : IDTO { public int Id { get; set; } public string FirstName { get; set; } } public class DTOb : DTOa, IDTO { public string Email { get; set; } public string Password { get; set; } } public class a { protected IDTO _DTO; public a() { _DTO = new DTOa(); } public int Id { get { return (_DTO as DTOa).Id; } set { (_DTO as DTOa).Id = value; } } public string FirstName { get { return (_DTO as DTOa).FirstName; } set { (_DTO as DTOa).FirstName = value; } } public DTOa ToValueObject() { return (_DTO as DTOa); } } public class b : a { public b() { _DTO = new DTOb(); } public string Email { get { return (_DTO as DTOb).Email; } set { (_DTO as DTOb).Email = value; } } public string Password { get { return (_DTO as DTOb).Password; } set { (_DTO as DTOb).Password = value; } } public DTOb ToValueObject() { return _DTO as DTOb; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload