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 wha
t
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:
> 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.com> wrote:
> 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 w
hat
> 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 settin
g
> 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 no
t
> available during constructor.
> HTH
> "slolife" wrote:
>
>
>
>
>
>
>
>
>
Monday, March 26, 2012
Okay to override an ASP.NET page's constructor
Labels:
asp,
aspnet,
base,
basepageinherits,
class,
classpublic,
codehere,
constructor,
m_needalerts,
net,
override,
page,
pageprotected,
system,
web
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment