• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

Best way to implement WebApplication-wide functions/variables?

Say I want a particular set of functions and variables to be available to all my webforms. Let's not worry about session state - they just require state for the current request of the current page.

I don't want to do this:

<code>
Public Class WebForm2
    Inherits System.Web.UI.Page
    Private MyUtilitiesClass as new UtilitiesClass()
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Response.Write (MyUtilitiesClass.BlahBlah())
    End Sub
End Class
</code>

Pretty sure making BLahBLah() a shared member won't work either.


The way I figured out seems pretty sneaky really, and maybe I'm missing a better way. I sorta cheated:

<code>
**FMS5.vb
Public Class FMS5
    Inherits System.Web.UI.Page
    Public Function db() As SqlClient.SqlConnection
        .
    End Function
    Public Function ExecuteReader(ByVal sql As String) As SqlClient.SqlDataReader
        .
    End Function
End Class

**WebForm2.aspx.vb
Public Class WebForm2
    Inherits FMS5

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        db.Open() ' db is declared in FMS5.vb
    End Sub
End Class

**WebForm3.aspx.vb
Public Class WebForm3
    Inherits FMS5

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        set r = ExecuteReader("select...")
    End Sub
End Class
</code>

IOW - I made my own "base" class, inheriting from the Page, and hte actual pages now inherit from my own base class. So they get my functions, and the get teh Page.* functions too.


Is this considered wrong, and is there another way to do it?
0
blowfly
Asked:
blowfly
1 Solution
 
b1xml2Commented:
You can encapsulate what you want to do into a class and expose that as a property in your base class.

Public NotInheritable Class MyFunctions

    Public Shared Function GetItem(ByVal bag As System.Web.UI.StateBag) As MyFunctions
        Return New MyFunctions(bag)
    End Function


    Private ReadOnly Property Page() As System.Web.UI.Page
        Get
            Return DirectCast(System.Web.HttpContext.Current.Handler, System.Web.UI.Page)
        End Get
    End Property

    Private ReadOnly bag As System.Web.UI.StateBag

    Private Sub New(ByVal bag As System.Web.UI.StateBag)
        Me.bag = bag
    End Sub

    Default Property Items(ByVal name As String) As Object
        Get
            Return bag(name)
        End Get
        Set(ByVal Value As Object)
            bag(name) = Value
        End Set
    End Property
    Public ReadOnly Property UserID() As String
        Get
            Return CStr(Page.Session("UserID"))
        End Get
    End Property


    Public ReadOnly Property Data() As Integer
        Get
            'using the view state
            Return CInt(IIf(Me("Data") Is Nothing, 0, Me("Data")))
        End Get
    End Property

End Class

Base page example:

Public Class BasePage
      Inherits System.Web.UI.Page
      
      Private f As MyFunctions
      
      Private Sub BasePage_Init(ByVal sender As Object,ByVal e As EventArgs) Handles MyBase.Init
            f = MyFunction.GetItem(Me.ViewState)
      End Sub
      
      Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            'e.g.
            Response.Write(MySpecialFunctions.UserID)
      End Sub
      
      Public ReadOnly Property MySpecialFunctions() As MyFunctions
            Get
                  Return f
            End Get
      End Property
End Class
0
 
Hamed ZaghaghiProgrammerCommented:
you can write all of your funxctions in global.asax.
0
 
b1xml2Commented:
I am not a fan of writing stuff to the Global.asax as this becomes available not just to all webforms but to all user controls...

by using the approach mentioned:

1. CUSTOM CLASS
2. BASE PAGE has CUSTOM CLASS as a PROPERTY

This way, you can use different base pages to provide common functionality. You can also write common utility classes that only have shared members and for which you can access anytime. There is really no need to play around with the Global class
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
blowflyAuthor Commented:
Sorry, I should have made it clearer. I said:

==================
I don't want to do this:
<code>
Public Class WebForm2
    Inherits System.Web.UI.Page
    Private MyUtilitiesClass as new UtilitiesClass()
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Response.Write (MyUtilitiesClass.BlahBlah())
    End Sub
End Class
</code>
==================

Reason being that i don't wan to prefix each function by "MyUtilitiesClass." all the time. Every now and then it's OK, but for heavily used functions it's a major nuisance. (Hence my attempt at fiddling with the webform's inheritance)

Coming from a heavily ASP/VB6 background, maybe I just need to adjust to the new way of doing things, but I could simply add a function to a VB6 module, and it was instantly accessible to everywhere does. I know that ain't object oriented, but it sure was nifty.

I'll try the global.asax thing, thanks.
0
 
blowflyAuthor Commented:
OK - if I add "Public MyPublicMember As String" to the GLobal class in Global.asax, how do I reference it in WebForm1? (Tried a few different things, no luck)
0
 
neil_richardsCommented:
What you're doing is one of the 'best practice' ways of doing it.  Alternatively you could create static methods within the UtilitiesClass, but then you'd have to go UtilitiesClass.BlahBlah() with the prefix (which you said you didn't want.  Otherwise, what you're doing is fine, creating a parent class which shares methods among it's children is classic Object Oriented design.

Just make sure to declare the methods and variables properly (ie. protected vs. private) so they're accessible from the children.

Cheers
0
 
blowflyAuthor Commented:
Thanks - as long as it's "recommended" and not some dodgy hack, I'm cool. Just wanted confirmation on that.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now