Okay to override an ASP.NET page's constructor

  • Thread starter Thread starter slolife
  • Start date Start date
S

slolife

See any problem with this code:

Here's my Base page class:

Public Class BasePage
Inherits System.Web.UI.Page

Protected m_NeedAlerts As Boolean

Private Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles MyBase.Init
Dim doSomethingWithThis As Boolean = m_NeedAlerts
End Sub

End Class

Now, in order to set m_NeedAlerts in a page that inherits from
BasePage, I need to do this, right?

Partial Class AddCertification
Inherits BasePage

Public Sub New()
MyBase.m_NeedAlerts = True
End Sub
End Class

Is there a problem with overriding the page's constructor? I've never
seen this done in all the code I've seen, so I am a little worried.
Is there a better way to do what I am trying to accomplich
 
slolife,

There is no problem with overriding constructor, as long as you put
MyBase.New as the first statement in it. There are a few ways to achieve what
you need (set up m_NeedAlerts from inherited page):
1 - the one you specified - override constructor and set it there
2- override OnInit method in inherited class, set the variable, and then
call MyBase.OnInit
3 - declare a MustOverride method in the base class that will force setting
the variable and call it from your Page_Init sub of the base class before
examining its value

The advantage of 2 & 3 over 1 would be that you may need to refer to
controls' properties to determine the value of your variable, which are not
available during constructor.

HTH
 
Back
Top