dophine

Hi,

I would like to know how to override an event. and I just found those ways is just for c# like to override the onXXXX where XXXX is the event. but I cannot find any in VB. so is there any way to do it in VB

Thank you



Re: Visual Basic Express Edition how to override an event

Omnicoder

Protected Override <eventname>

<code>

End sub






Re: Visual Basic Express Edition how to override an event

jo0ls

tip: type "overrides " where you want the function to appear and intellisense will barge in with a list to choose from, then fill in the function header for you.




Re: Visual Basic Express Edition how to override an event

dophine

Thank you for your reply.

but I don't understand how to choose the name. it always complains the function name cannot be declared as 'override' since it does not override a sub int the base class.

for example, if I want to override the click event of a botton, what should the name be





Re: Visual Basic Express Edition how to override an event

jo0ls

In OOP, when you have a class that inherits a behaviours from another class, sometimes you don't want to use that behaviour - you want to override it and use your own.

So the first important part is that you would declare an override in a Class, and it is overriding something that is declared in the parent Class.

Public Class MyBaseClass
  Public Overridable Sub Hello()
    ' base class version
  End Sub
End Class
 
Public Class MyChildClass
  Inherits MyBaseClass
  Overrides Sub Hello()
    ' we override the version inherited from MyBaseClass
  End Sub
End Class

Now, you say you want to override the click event of a button, you would do this when your base class is a Form with a Button on it and an event handler, and you are writing your code in a Class that inherits the Form. This would be allowed.

Here is an example - first the base class. To enable overriding of the click event handler, I've changed it to Protected - so that inheritors can see it, and overridable, so that they can override it.

Public Class Form1
 
  Private WithEvents b As New Button
 
  Sub New()
    InitializeComponent()
    Me.Controls.Add(b)
  End Sub
 
  ' normally it would be private and not overridable
  Protected Overridable Sub b_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles b.Click
    Me.Text = "BaseClass Says Hello"
    ' create a Class1 so we can test it
    Dim f As New Class1
    f.Show()
  End Sub
 
End Class

So that is the method that we are going to override. Overriding b_Click on Form1 makes no sense, Overriding is something you do to an inherited behaviour. As the method is declared on Form1, you can't override it there.

A Child Class can override b_click:

Public Class Class1
  Inherits Form1
 
  Protected Overrides Sub b_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    ' MyBase.b_Click(sender, e) ' we don't want to call the base class event
    Me.Text = "Class1 says hello"
  End Sub
End Class

When you run this project, with Form1 as the startup form, the button click on Form1 is handled by the code in Form1, and displays "BaseClass says hello". Then when you click the button on the instance of class1, it is handled by its overrides version so it displays "Class1 says hello".
If you comment out b_Click in Class1 and run it again, then this time the button on the instance of Class1 will use the inherited behaviour and display "BaseClass says hello".







Re: Visual Basic Express Edition how to override an event

dophine

Thank you for your explaination.



Re: Visual Basic Express Edition how to override an event

JohnWein

This is the clearest explanation of overriding I have seen. This post is just to bump it up.



Re: Visual Basic Express Edition how to override an event

Carsten Kanstrup

dophine

What exactly do you want to achive by overriding the event I may not be able to help you, but I find the question interesting.

An event is not a subroutine, but a delegate, which has an invocation list consisting of event handlers. When you click a button, a message gets posted to the message queue. My guess is that this message is called WM_CLICK, but it is only a guess! This message is in fact a delegate, which points to a method, which shall be executed. My guess is that this method is called OnClick. It may not be correct, but let us call it that. OnClick may not do much, but one of the last things it does is to raise the Click event by means of "RaiseEvent Click". Actually, this does not raise any events at all. RaiseEvent is just a replacement for delegate.Invoke, so it just executes all event handlers for the click event immediately.

If you want to change the behaviour of the button, so that it e.g. toggles between raising and On and Off event, you must override OnClick and this method is in fact overridable. However, if you only want to execute a new event handler, it is much easier just to remove the old one(s) from the invocation list (RemoveHandler) and then add a new one (AddHandler). I see no reason why you want to override an event handler, but I also have a feeling that this is not what your question was about.