Showing posts with label sub. Show all posts
Showing posts with label sub. Show all posts

Monday, March 26, 2012

Okay to override an ASP.NET page's constructor

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 accomplichslolife,

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
"slolife" wrote:

Quote:

Originally Posted by

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


That's for the advice Sergey. I went with the constructor for now.

On May 3, 8:49 pm, Sergey Poberezovskiy
<SergeyPoberezovs...@.discussions.microsoft.comwrote :

Quote:

Originally Posted by

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
>
"slolife" wrote:

Quote:

Originally Posted by

See any problem with this code:


>

Quote:

Originally Posted by

Here's my Base page class:


>

Quote:

Originally Posted by

Public Class BasePage
Inherits System.Web.UI.Page


>

Quote:

Originally Posted by

Protected m_NeedAlerts As Boolean


>

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

End Class


>

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

Partial Class AddCertification
Inherits BasePage


>

Quote:

Originally Posted by

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


>

Quote:

Originally Posted by

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