I have trouble understanding, when I have to override and why I have to override. I'm using a WebUserControl

I tried to use

Protected Sub Overrides OnPreRender(ByVal writer As System.Web.UI.HtmlTextWriter)

but then I get the error

sub 'OnPreRender' cannot be declared 'Overrides' because it does not override a sub in a base class.

And when I removed Overrides I get the warning

sub 'OnPreRender' shadows an overridable method in the base class 'Control'. To override the base method, this method must be declared 'Overrides'

and the event doesn't execute.

When I changed the name to

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

the event executes. Is there a Render event in the base of the WebUserControl to Override?

I have trouble to understand why I have to override an event and not use the event as it is. I would be glad if someone could explain all of this thoroughly.

Many thanks

7 Years
Discussion Span
Last Post by __avd

You were not providing the proper method signature when you were attempting to override OnPreRender. The proper signature is the following

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)

Overriding with the invalid signature didn't work because there was nothing to override with that signature. Omitting "overrides" and accepting a different parameter type did not work because no event is raised providing the type of argument your function accepted.

Provide the right signature, get the right result.


>I have trouble understanding, when I have to override and why I have to override.

From the wiki:
Method overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.

When the base class virtual methods are not adequate for your classes then you may replace these unwanted base class methods by using this (override) mechanism.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.