I have a general question about inheritance in the .NET framework, lets say you have 2 classes, the first is called Parent and the second is called Child. Child inherits from Parent.
Parent wants to ensure that each instance of child executes a specific piece of code when it loads irrespective of whether the child has their own onLoad code explicitly specified.
From my experience, if i handle onLoad in the parent and not in the child, the parents onLoad code will fire but if its handled in both classes only the child’s code will fire.
Is this correct? and if so how can i ensure the parents code will always fire for the child…
You have discovered the fragile base class problem. One way to deal with it is this pattern:
The parent is guaranteed to have it’s OnLoad method do what it is supposed to do, but the child is given a way to add additional processing by overriding OnLoadInternal.