all groups > dotnet windows forms > april 2005 >
You're in the

dotnet windows forms

group:

Inherited code and derived code.


Inherited code and derived code. dbuchanan
4/28/2005 6:51:38 AM
dotnet windows forms: Can inherited code call derived code? If so how.
I have identical 'generic' code that I am repeating again and again
in several derived form because I don't know how to get inherited
code to call derived code. Am I stuck with this situation or is there a
way around it? Below is some sample code.
==============================
Private Sub LoadDataInForm() '= Form_Load
Call FillDataSet()
Call CreateBindings()
Call DataEntryControlsAccessible(False)
Call HideDgColumns()
Call NewCancelSaveCloseButtonState("NewClose")
End Sub

Private Sub RemoveRow() ' RemoveAt button
_bmb.RemoveAt(_bmb.Position)
Call DataEntryControlsAccessible(False)
Call NewCancelSaveCloseButtonState("NewClose")
Call UpdateDataSet()
End Sub
==============================
Most of the calls call code in the derived forms.
Is there a way around duplicating this code in every form?

- Doug
Re: Inherited code and derived code. Jay B. Harlow [MVP - Outlook]
4/28/2005 9:04:14 AM
dbuchanan,
| Can inherited code call derived code? If so how.
It sounds like you wan to use the Template Method pattern.

In your base form define the method to call Overridable methods that the
derived form overrides.

Something like:

Public Class BaseForm
Inherits System.Windows.Forms.Form

| Private Sub LoadDataInForm() '= Form_Load
| Call FillDataSet()
| Call CreateBindings()
| Call DataEntryControlsAccessible(False)
| Call HideDgColumns()
| Call NewCancelSaveCloseButtonState("NewClose")
| End Sub

Protected Overridable Sub FillDataSet()
Throw New NotImplementedException()
End Sub

Protected Overridable Sub CreateBindings()
Throw New NotImplementedException()
End Sub

...

End Class

Public Class DerivedForm
Inherits BaseForm

Protected Overrides Sub FillDataSet()
' fill the data set object
End Sub

Protected Overrides Sub CreateBindings()
' create the data bindings
End Sub

...

End Class

Depending on the requirements of the Template method & the base class I will
make the stub routines (FillDataSet & CreateBindings) MustOverride instead
of Overridable as the derived class is required to override them, however in
the case of a Form, you cannot use MustOverride as it interferes with the
designer... using a couple "#if debug" you can have the method MustOverride
in the release builds & Overridable in the debug builds...

Hope this helps
Jay


[quoted text, click to view]
| Can inherited code call derived code? If so how.
| I have identical 'generic' code that I am repeating again and again
| in several derived form because I don't know how to get inherited
| code to call derived code. Am I stuck with this situation or is there a
| way around it? Below is some sample code.
| ==============================
| Private Sub LoadDataInForm() '= Form_Load
| Call FillDataSet()
| Call CreateBindings()
| Call DataEntryControlsAccessible(False)
| Call HideDgColumns()
| Call NewCancelSaveCloseButtonState("NewClose")
| End Sub
|
| Private Sub RemoveRow() ' RemoveAt button
| _bmb.RemoveAt(_bmb.Position)
| Call DataEntryControlsAccessible(False)
| Call NewCancelSaveCloseButtonState("NewClose")
| Call UpdateDataSet()
| End Sub
| ==============================
| Most of the calls call code in the derived forms.
| Is there a way around duplicating this code in every form?
|
| - Doug
|

AddThis Social Bookmark Button