Watz
Watz

Reputation: 849

Solution to re-use child class method within its siblings (ASP.NET/VB.NET)

I have 5 web forms which contains same user controls with different functionality. The current system follows MVP pattern. All these web forms inherited from a single base class (Please refer the following image.). Now, I have to disable each and every form individually for a certain condition which is common for all the forms. So, I wrote a method for one of the forms to disable each and every field in its user controls and it works fine. I can re-use this method for the rest of the web forms as they all contain the same set of user controls. enter image description here

The DisableControls() method calls DisableInputs() method of each user control:

enter image description here

This would be duplicated in other web form classes. Does anyone can suggest a suitable solution to re-use this method?

Upvotes: 0

Views: 49

Answers (1)

Jesse Potter
Jesse Potter

Reputation: 847

If you include an empty sub in your base class for DisableControls() you can override this in the child classes without having to give the base class specific knowledge of the child classes.

Something like this:

Public Class Base
    Overridable Sub DisableControls()
        'Do nothing if nothing is implemented
    End Sub
End Class

Public Class Instance1
    Inherits Base
    Public Overrides Sub DisableControls()
        Me.UserControl1.DisableInputs()
        Me.UserControl2.DisableInputs()
    End Sub
End Class

Public Class Instance2
    Inherits Base
    Public Overrides Sub DisableControls()
        Me.UserControl3.DisableInputs()
    End Sub
End Class

Upvotes: 1

Related Questions