Hi
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
Fia

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.