Results 1 to 5 of 5

Thread: VB.NET Web Forms - Using the application object to store a custom class

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    2

    Post VB.NET Web Forms - Using the application object to store a custom class

    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

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: VB.NET Web Forms - Using the application object to store a custom class

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,594

    Re: VB.NET Web Forms - Using the application object to store a custom class

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    2

    Re: VB.NET Web Forms - Using the application object to store a custom class

    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

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,594

    Re: VB.NET Web Forms - Using the application object to store a custom class

    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
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width