Inherited code and derived code.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dbuchanan

    Inherited code and derived code.

    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 DataEntryContro lsAccessible(Fa lse)
    Call HideDgColumns()
    Call NewCancelSaveCl oseButtonState( "NewClose")
    End Sub

    Private Sub RemoveRow() ' RemoveAt button
    _bmb.RemoveAt(_ bmb.Position)
    Call DataEntryContro lsAccessible(Fa lse)
    Call NewCancelSaveCl oseButtonState( "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

  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Inherited code and derived code.

    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 DataEntryContro lsAccessible(Fa lse)
    | Call HideDgColumns()
    | Call NewCancelSaveCl oseButtonState( "NewClose")
    | End Sub

    Protected Overridable Sub FillDataSet()
    Throw New NotImplementedE xception()
    End Sub

    Protected Overridable Sub CreateBindings( )
    Throw New NotImplementedE xception()
    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


    "dbuchanan" <dbuchanan52@ho tmail.com> wrote in message
    news:1114696298 .809340.189460@ o13g2000cwo.goo glegroups.com.. .
    | 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 DataEntryContro lsAccessible(Fa lse)
    | Call HideDgColumns()
    | Call NewCancelSaveCl oseButtonState( "NewClose")
    | End Sub
    |
    | Private Sub RemoveRow() ' RemoveAt button
    | _bmb.RemoveAt(_ bmb.Position)
    | Call DataEntryContro lsAccessible(Fa lse)
    | Call NewCancelSaveCl oseButtonState( "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
    |


    Comment

    • dbuchanan

      #3
      Re: Inherited code and derived code.

      Jay,

      It works!

      You refer to the "template method" pattern. Where can I learn more
      about this and other patterns?

      Thank you.
      -Doug

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Inherited code and derived code.

        Doug,
        The "Template Method" pattern is defined in the Gang of Four's (GOF) book
        "Design Patterns - Elements of Reusable Object-Oriented Software" from
        Addison Wesley, it is IMHO a "must have" book for the serious OO developer.
        Design Patterns provide programmers with a convenient way to reuse
        object-oriented code & concepts amount programmers and across projects,
        offering easy, time-saving solutions to commonly recurring problems in
        software design. The GOF are Erich Gamma, Richard Helm, Ralph Johnson, and
        John Vlissides.

        James W. Cooper's book "Visual Basic Design Patterns - VB 6.0 and VB.NET" is
        also a "must have" book that is an excellent companion to the above GOF
        book. Cooper's book gives the VB6 & VB.NET view of each pattern in the GOF
        book.

        I would ultimately recommend both books. However! If you can't afford both
        of the above books I would recommend the first if you can read & understand
        C++, otherwise I would recommend the second if you only can read &
        understand VB...

        The C# version of Cooper's book was available on the internet, not sure if
        it still is. There are numerous sites (some better then others) that cover
        the above design patterns & others...

        Hope this helps
        Jay


        "dbuchanan" <dbuchanan52@ho tmail.com> wrote in message
        news:1114981783 .808893.131090@ o13g2000cwo.goo glegroups.com.. .
        | Jay,
        |
        | It works!
        |
        | You refer to the "template method" pattern. Where can I learn more
        | about this and other patterns?
        |
        | Thank you.
        | -Doug
        |


        Comment

        Working...