Results 1 to 4 of 4

Thread: [RESOLVED] Understanding the effect of not calling Mybase.OnWhatever within OnWhatever override

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    the Netherlands
    Posts
    18

    Resolved [RESOLVED] Understanding the effect of not calling Mybase.OnWhatever within OnWhatever override

    Hi,

    This question is not about a particular class or a particular event, but about a gereral principle.
    I don't fully understand how overriding OnWhatever modifies the Whatever event, when it comes to calling MyBase.OnWhatever .

    This random example subclasses Textbox and overrides OnKeyPress. I expect this to prevent the modifiedTextbox from processing the KeyPress event, because it doesn't call MyBase.OnKeyPress(e) ...
    vb.net Code:
    1. Class modifiedTextbox
    2.     Inherits TextBox
    3.     Protected Overrides Sub OnKeyPress(e As System.Windows.Forms.KeyPressEventArgs)
    4.         ' deleted  call to MyBase.OnKeyPress(e)
    5.     End Sub
    6. End Class
    ... but the event is processed as usual.

    So, I have two questions:
    Why is the event processed anyhow?
    What is the effective difference between calling and not calling MyBase.OnKeyPress(e) ?

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Location
    the Netherlands
    Posts
    18

    Re: [RESOLVED] Understanding the effect of not calling Mybase.OnWhatever within OnWha

    I seem to get it all wrong.
    Not calling MyBase.OnKeyPress(e) only prevents the event consumer from receiving the event.

    Still, is there a way I can modify event behavior?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] Understanding the effect of not calling Mybase.OnWhatever within OnWha

    You should probably follow the Blog link in my signature below and check out my post on Custom Events to see how the convention is implemented. Basically, if you have a Whatever event in a class then it's the OnWhatever method in that class that raises that event. If you override OnWhatever in a derived class and don't call the base implementation then the event won't be raised. The point of overriding the method is to add extra behaviour to the event. In the overridden OnWhatever method you first add any code that you want executed before the event is raised, then you call the base implementation to raise the event and execute any handlers registered for the event, then you add any code that you want executed after the event is raised.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] Understanding the effect of not calling Mybase.OnWhatever within OnWha

    To get a clear idea of what happens, try running the example below. Create a new WinForms project, add three Buttons to your form and then add the following code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private WithEvents parent As New Parent
    4.     Private WithEvents child As New Child
    5.     Private WithEvents grandchild As New Grandchild
    6.  
    7.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    8.         parent.Text = "Hello World"
    9.     End Sub
    10.  
    11.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    12.         child.Text = "Hello World"
    13.     End Sub
    14.  
    15.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    16.         grandchild.Text = "Hello World"
    17.     End Sub
    18.  
    19.     Private Sub parent_TextChanged(sender As Object, e As EventArgs) Handles parent.TextChanged
    20.         Debug.WriteLine("Handling event for Parent")
    21.     End Sub
    22.  
    23.     Private Sub child_TextChanged(sender As Object, e As EventArgs) Handles child.TextChanged
    24.         Debug.WriteLine("Handling event for Child")
    25.     End Sub
    26.  
    27.     Private Sub grandchild_TextChanged(sender As Object, e As EventArgs) Handles grandchild.TextChanged
    28.         Debug.WriteLine("Handling event for Grandchild")
    29.     End Sub
    30.  
    31. End Class
    32.  
    33.  
    34. Public Class Parent
    35.  
    36.     Private _text As String
    37.  
    38.     Public Event TextChanged As EventHandler
    39.  
    40.     Public Property Text As String
    41.         Get
    42.             Return _text
    43.         End Get
    44.         Set
    45.             If _text <> Value Then
    46.                 _text = Value
    47.                 OnTextChanged(EventArgs.Empty)
    48.             End If
    49.         End Set
    50.     End Property
    51.  
    52.     Protected Overridable Sub OnTextChanged(e As EventArgs)
    53.         Debug.WriteLine("Raising event in Parent")
    54.         RaiseEvent TextChanged(Me, e)
    55.     End Sub
    56.  
    57. End Class
    58.  
    59.  
    60. Public Class Child
    61.     Inherits Parent
    62.  
    63.     Protected Overrides Sub OnTextChanged(e As EventArgs)
    64.         Debug.WriteLine("Pre-event behaviour in Child")
    65.         MyBase.OnTextChanged(e)
    66.         Debug.WriteLine("Post-event behaviour in Child")
    67.     End Sub
    68. End Class
    69.  
    70.  
    71. Public Class Grandchild
    72.     Inherits Child
    73.  
    74.     Protected Overrides Sub OnTextChanged(e As EventArgs)
    75.         Debug.WriteLine("Pre-event behaviour in Grandchild")
    76.         MyBase.OnTextChanged(e)
    77.         Debug.WriteLine("Post-event behaviour in Grandchild")
    78.     End Sub
    79. End Class
    Run the project and watch the Output window while you click the three Buttons and you'll see what code gets executed when.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width