# .NET and More > ASP.NET And ASP.NET Core >  VB.NET Web Forms - Using the application object to store a custom class

## Kapowie

Using vb.net (2022 - community edition)

I have a custom class that has Methods and properties I want available to every web page. Used for things such as Database reads and writes, Constants, Application level variables.

*Can I use the Web Forms Application Object to store the class so that it is created once, stored in memory once and can access from any web page?*

This is what I am currently doing and it works fine.

MY class Object 
Public Class Utilities
   Public Function Foo() As String
     Return "FooBar"
    End Function
End Class

What I am currently doing is the following - and it works fine. Just trying to determine if somehow the APPLICATION Object in Web forms can be used to make more efficient code - *all answers, thought and comments are welcome.*

Public Class HelloWorld
    Inherits System.Web.UI.Page
    Public u As New WebApplication1.Utilities
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Label1.Text = u.Foo()
    End Sub
End Class

----------


## techgnome

Personally I wouldn't do that. I can't remember if the Application object is thread safe, something tells me that it isn't. I could be wrong about that. Even if it is, I don't think you really gain anything by putting it in there. 


-tg

----------


## sapator

I'm guessing ASP.NET standard?
You can use HttpContext.Cache and you can access it globally. The issue here is to store smart. If you are going to store a large object then there are other ways.
Not that the cache will be removed occasionally so you need to first check if there is something cached and then use it.

----------


## Kapowie

I thank you both for your replies.  Could you critique the way I am doing it now? Is there anything wrong with it? Its the way I have done it before and it has always worked.  However, I am never satisfied with doing things just because they work work, I want code that is memory efficient and easy to maintain.

To me it looks like the application is only capable of storing strings based on most of the sample code I have seen - something like below.

Application["WebStoreName"] = "The Widget Store"

Can the Application Object store more than just strings?  If it can't store more complex objects or an instance of class than it eliminates the application object as a way to create a globally accessible Class with Methods required on all aspx - web-pages.  The class I will use will have SQL DB reads, DB Writes and helper functions.

Is there any better way than INSTANTIATING a CLASS on every page?

Thanks for your answers so far and many thanks for any future answers.

~howie

----------


## sapator

Somewhat similar but different, didn't use application back when I was programing, I remember it was an extreme situation as leaving objects in server memory without expiration often bloated the apps. Note I was in a programming company so the apps where not home made simple ones but multi used from thousand of users.
Anyhow, take your pick:
https://stackoverflow.com/questions/...ssion-vs-cache

----------

