开发者

Inherit same Method for 2 Different Class

开发者 https://www.devze.com 2023-01-31 02:44 出处:网络
How to simplify this, 2 method in class are identical. First class. Public Class Gui Inherits System.Web.UI.Page

How to simplify this, 2 method in class are identical.

First class.

Public Class Gui
Inherits System.Web.UI.Page


Public Sub CreateStyleLink(ByVal StyleArray() As String)
    Dim StrStyleLink As String

    For Each StyleName In StyleArray
        StrStyleLink = [Global].ArrStyles(StyleName)

        Dim ObjScript As New HtmlGenericControl("link")
        ObjScript.Attributes.Add("rel", "Stylesheet")
        ObjScript.Attributes.Add("type", "text/css")
        ObjScript.Attributes.Add("href", StrStyleLink)
        Me.Page.Header.Controls.Add(ObjScript)
    N开发者_如何转开发ext
End Sub


Public Sub CreateScriptLink(ByVal ScriptArray() As String)
    Dim ScriptLink As String

    For Each ScriptName In ScriptArray
        ScriptLink = [Global].ArrScripts(ScriptName)

        Dim ObjScript As New HtmlGenericControl("script")
        ObjScript.Attributes.Add("type", "text/javascript")
        ObjScript.Attributes.Add("src", ScriptLink)
        Me.Page.Header.Controls.Add(ObjScript)
    Next
End Sub


Public Sub SetSelectedMenu(ByVal MenuName As String, Optional ByRef ObjPage As Page = Nothing)
    Dim ObjMenu As New HtmlGenericControl

    If ObjPage Is Nothing Then
        ObjMenu = MyBase.Page.FindControl(MenuName)
    Else
        ObjMenu = ObjPage.FindControl(MenuName)
    End If
    ObjMenu.Attributes.Add("class", "selected")
End Sub


Public Sub ExecuteScript(ByVal ScriptName As String, Optional ByVal ScriptParam As String = "")
    Dim requestSM As ScriptManager = ScriptManager.GetCurrent(Me.Page)
    If Not requestSM Is Nothing And requestSM.IsInAsyncPostBack Then
        ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, ScriptName, ScriptName + "('" + ScriptParam + "');", True)
    Else
        Me.Page.ClientScript.RegisterStartupScript(Me.GetType, ScriptName, ScriptName + "('" + ScriptParam + "');", True)
    End If
End Sub

End Class

Second class.

Public Class GuiControl
Inherits System.Web.UI.UserControl


Public Sub CreateStyleLink(ByVal StyleArray() As String)
    Dim StrStyleLink As String

    For Each StyleName In StyleArray
        StrStyleLink = [Global].ArrStyles(StyleName)

        Dim ObjScript As New HtmlGenericControl("link")
        ObjScript.Attributes.Add("rel", "Stylesheet")
        ObjScript.Attributes.Add("type", "text/css")
        ObjScript.Attributes.Add("href", StrStyleLink)
        Me.Page.Header.Controls.Add(ObjScript)
    Next
End Sub


Public Sub CreateScriptLink(ByVal ScriptArray() As String)
    Dim ScriptLink As String

    For Each ScriptName In ScriptArray
        ScriptLink = [Global].ArrScripts(ScriptName)

        Dim ObjScript As New HtmlGenericControl("script")
        ObjScript.Attributes.Add("type", "text/javascript")
        ObjScript.Attributes.Add("src", ScriptLink)
        Me.Page.Header.Controls.Add(ObjScript)
    Next
End Sub


Public Sub ExecuteScript(ByVal ScriptName As String, Optional ByVal ScriptParam As String = "")
    Dim requestSM As ScriptManager = ScriptManager.GetCurrent(Me.Page)
    If Not requestSM Is Nothing And requestSM.IsInAsyncPostBack Then
        ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, ScriptName, ScriptName + "('" + ScriptParam + "');", True)
    Else
        Me.Page.ClientScript.RegisterStartupScript(Me.GetType, ScriptName, ScriptName + "('" + ScriptParam + "');", True)
    End If
End Sub
End Class


Create a base (possibly abstract) class that defines the method and inherit from that.

Alternatively you could use an extension method.


I would create these as either simple static members of a utility class or if you prefer you could extend the static members to be extension methods of UserControl

Here is a quick example of a utility class, Not tested and VB is not the language I work with most so excuse any minor syntax oversights.

Class NotInheritable UserControlHelper
    Public Sub CreateStyleLink(ByVal control as UserControl, _
      ByVal StyleArray() As String)
        Dim StrStyleLink As String

        For Each StyleName In StyleArray
            StrStyleLink = [Global].ArrStyles(StyleName)

            Dim ObjScript As New HtmlGenericControl("link")
            ObjScript.Attributes.Add("rel", "Stylesheet")
            ObjScript.Attributes.Add("type", "text/css")
            ObjScript.Attributes.Add("href", StrStyleLink)
            control.Page.Header.Controls.Add(ObjScript)
        Next
    End Sub


    Public Sub CreateScriptLink(ByVal control as UserControl, _
      ByVal ScriptArray() As String)
        Dim ScriptLink As String

        For Each ScriptName In ScriptArray
            ScriptLink = [Global].ArrScripts(ScriptName)

            Dim ObjScript As New HtmlGenericControl("script")
            ObjScript.Attributes.Add("type", "text/javascript")
            ObjScript.Attributes.Add("src", ScriptLink)
            control.Page.Header.Controls.Add(ObjScript)
        Next
    End Sub

    Private Sub New()
    End Sub
End Class

This can then be used from anywhere that you have a UserControl to pass in as the first argument. I am not sure if extension methods can be defined in VB.NET, I thing 2010 supports this but I might be wrong.


Creating a base class just to inherit a common method often is not the way to go. Inheritance is meant to create a more specific type of object, not just for sharing. In this case one is a Page, the other is a UserControl, which probably don't define a hierarchical relationship.

As such, a utility or helper class would probably be the route to go, basically as Chris described. One addition i'd make would be to make the two methods Shared, so as to not need an instance just to use.

By using a normal instance method, you'd have to go something along this route:

Dim help As New UserControlHelper
help.CreateScriptLink(Me)

as opposed to a Shared method, could could be done just as:

UserControlHelper.CreateScriptLink(Me)

I don't recall if Page inherits from UserControl (or vice-versa), so you may need to find a higher class to pass into the method, but the idea is basically the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消