Home > Net >  How to get parrent object from child
How to get parrent object from child

Time:12-07

I need a litle bit of help with .net. I have this View mode. lets say A and it contains List of B items ( class A { public List< B > BItems }; class B{ A AItem{set; get;} string Name{get; set;} }). When I pass model from view it fills A class correctly (There are number of BItems and these B Items has correct names), problem is in B class AItem is allways null. How I dind List of objects that all these List items would point to its parrent ?

CodePudding user response:

You can't do that since the child has no reference to the parent at all and it's a Bad Idea(TM) to begin with.

Classes should not know anything at all about other classes in the hierarchy that you've come up with. This keeps to the philosophy of "separation of concerns." This makes it possible for you to reuse classes without having them dependent on each other.

TO do inheritance properly, a child class needs to "extend" a parent class.

  • Related