When inheriting from System.Web.UI.Page or UserControl, do I have tocall their contructors?

D

DotNetNewbie

Hi,

When inheriting from System.Web.UI.Page (or any other class for that
matter), do I have to call the inherited classes constructors?

I've seen allot of code where people do:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}


Is this necessary if we don't override any methods/events of the
inherited class also?
 
J

Jon Skeet [C# MVP]

DotNetNewbie said:
When inheriting from System.Web.UI.Page (or any other class for that
matter), do I have to call the inherited classes constructors?

I've seen allot of code where people do:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}

That's probably just created by the designer. It's not doing anything
useful.

Note that that *isn't* calling the constructor - it's calling the
OnInit method.
 
N

Nicholas Paldino [.NET/C# MVP]

The OnInit is useful. If it isn't called, then themes and stylesheets
generated in code or through ASP.NET control markup will not be applied.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
The OnInit is useful. If it isn't called, then themes and stylesheets
generated in code or through ASP.NET control markup will not be applied.

But surely overriding OnInit to just call base.OnInit isn't useful is
it? How can that change anything?
 
N

Nicholas Paldino [.NET/C# MVP]

I thought you were referring to just the call to OnInit in the override.
The assumption was that there was some other code after the call to OnInit.
If that's all there is in the method definition, then yes, it's pretty
pointless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top