locked
Problem with Inheritance RRS feed

  • Question

  • Hello,

     

    I have a large problem with inheritance in vb.net.

    The problem is the following:

    I have 2 forms => frmBase and frmChild

     

    In frmBase i want to create a method Called StartWorking() and i want frmChild to inherit this method.

    But here is the tricky thing: when frmChild.StartWorking is called i would like the following => without calling MyBase.StartWorking()

    i want frmBase.StartWorking() to be executed first and after a test in frmBase.StartWorking if blnValue is true then frmChild.StartWorking has to be activated. if blnValue is false that frmChild.StartWorking cannot be activated.


    Please help me i have been searching for an answer to this for the entire week but didin't find a sollution.

     

    Friday, November 25, 2011 10:48 AM

Answers

  • While an overrides method would do the immediate job there is another way to accomplish this that I think may be more responsible-- you could use a protected event like the code below where the method is publically called upon the base class and the inheritors can choose to respond to the success of the method by handling the event, like this:

    Public Class frmBase
        Inherits Form
    
        Protected Event WorkingStarted(e As EventArgs)
    
        Public Sub StartWorking()
            'Run some process...
            If (SomeCondition = True) Then
                RaiseEvent WorkingStarted(EventArgs.Empty)
            End If
        End Sub
    
    End Class
    
    Public Class frmChild
        Inherits frmBase
    
        Private Sub frmChild_WorkingStarted(e As System.EventArgs) Handles Me.WorkingStarted
            MsgBox("Working has started - handled by child form.")
        End Sub
    
    End Class
    

    The problem I have with the overrides method suggested by ScanFaN is this:  The base method is a function that returns boolean specifically to tell the inheriting class to do something. Worse, it returns True to trigger another action but a return of False has no explicit value.  This already smells afoul.  Also, base classes should never have any awareness of inheritors and should never be coded that way.  Why?  Because there are "n" number of possible inheritors and they should by definition have slightly different behaviors that cannot be predicted by the base class.  So a return value of True may mean something else to the next inheriting class, possibly something opposite even.  Likely - No.  But possible - yes.  And that is bad design.

    So If the reason behind the boolean return is to just indicate success then an event is a much better option because it explicitly is called when a certain state is achieved and does not attempt to understand how the subscribers will react to the event being raised.  This is a subtle difference but important when you start designing more intricate systems of inheritance where coding time and resource wasting anti-patterns can lead to poor maintainability and difficulty in debugging and testing.

     

     

    • Proposed as answer by xxxScanFaNxxx Tuesday, November 29, 2011 4:33 PM
    • Marked as answer by Mike Feng Monday, December 5, 2011 7:30 AM
    Saturday, November 26, 2011 12:16 AM
  • You probably want something that looks like this:-

    Public Class frmBase
        Inherits Form
    
        Public Overridable Function StartWorking() As Boolean
    
        End Function
    
    End Class
    
    Public Class frmChild
        Inherits frmBase
    
        Public Overrides Function StartWorking() As Boolean
            If MyBase.StartWorking() Then
                'Implement frmChild's version of StartWorking()
            End If
        End Function
    
    End Class
    


    • Proposed as answer by Sygrien Friday, November 25, 2011 4:00 PM
    • Marked as answer by Mike Feng Monday, December 5, 2011 7:30 AM
    Friday, November 25, 2011 11:58 AM

All replies

  • You probably want something that looks like this:-

    Public Class frmBase
        Inherits Form
    
        Public Overridable Function StartWorking() As Boolean
    
        End Function
    
    End Class
    
    Public Class frmChild
        Inherits frmBase
    
        Public Overrides Function StartWorking() As Boolean
            If MyBase.StartWorking() Then
                'Implement frmChild's version of StartWorking()
            End If
        End Function
    
    End Class
    


    • Proposed as answer by Sygrien Friday, November 25, 2011 4:00 PM
    • Marked as answer by Mike Feng Monday, December 5, 2011 7:30 AM
    Friday, November 25, 2011 11:58 AM
  • While an overrides method would do the immediate job there is another way to accomplish this that I think may be more responsible-- you could use a protected event like the code below where the method is publically called upon the base class and the inheritors can choose to respond to the success of the method by handling the event, like this:

    Public Class frmBase
        Inherits Form
    
        Protected Event WorkingStarted(e As EventArgs)
    
        Public Sub StartWorking()
            'Run some process...
            If (SomeCondition = True) Then
                RaiseEvent WorkingStarted(EventArgs.Empty)
            End If
        End Sub
    
    End Class
    
    Public Class frmChild
        Inherits frmBase
    
        Private Sub frmChild_WorkingStarted(e As System.EventArgs) Handles Me.WorkingStarted
            MsgBox("Working has started - handled by child form.")
        End Sub
    
    End Class
    

    The problem I have with the overrides method suggested by ScanFaN is this:  The base method is a function that returns boolean specifically to tell the inheriting class to do something. Worse, it returns True to trigger another action but a return of False has no explicit value.  This already smells afoul.  Also, base classes should never have any awareness of inheritors and should never be coded that way.  Why?  Because there are "n" number of possible inheritors and they should by definition have slightly different behaviors that cannot be predicted by the base class.  So a return value of True may mean something else to the next inheriting class, possibly something opposite even.  Likely - No.  But possible - yes.  And that is bad design.

    So If the reason behind the boolean return is to just indicate success then an event is a much better option because it explicitly is called when a certain state is achieved and does not attempt to understand how the subscribers will react to the event being raised.  This is a subtle difference but important when you start designing more intricate systems of inheritance where coding time and resource wasting anti-patterns can lead to poor maintainability and difficulty in debugging and testing.

     

     

    • Proposed as answer by xxxScanFaNxxx Tuesday, November 29, 2011 4:33 PM
    • Marked as answer by Mike Feng Monday, December 5, 2011 7:30 AM
    Saturday, November 26, 2011 12:16 AM