How to declare an application-wide static variable in (VB)

Posted on 2011-10-27
Last Modified: 2012-05-12
Hi (VB) experts,

for my (VB) application I would like to declare a variable that is globally shared between all sessions, which will be used for some low-level system task. Just to make sure: I want that if one session changes the value, it is also changing in another session of the web site.

I have googled around, but many posts refer to "static" declarations, others to "global" or "shared", others yet mention that c# "static" is the same as vb "shared". Then I don't quite understand where (i.e. in which file in which directory, like App_Code etc.) and how (class or module or whatever) I should define such a variable. I'm totally confused.

Could someone please help me understand this and tell me how I can define a string variable named "status" that is accessible throughout the application, i.e. with the same shared content in all sessions? Some code example would be highly appreciated. (I am aware that I haven't fully understood the life-cycle and inheritance issues here, but for the time being, I'm happy with a kick-start on this globally-shared-variable-issue.)

Thank you in advance,
Question by:Staudte
    LVL 5

    Accepted Solution


    To Share the data with all application users, you can use ASP.NET Application object. Given is the sample code to access Application object in ASP.NET:

    Dim htblGlobalValues As Hashtable = Nothing

    If Application("GlobalValueKey") IsNot Nothing Then
          htblGlobalValues = TryCast(Application("GlobalValueKey"), Hashtable)
          htblGlobalValues = New Hashtable()
    End If

    htblGlobalValues.Add("Key1", "Value1")
    htblGlobalValues.Add("Key2", "Value2")

    Me.Application("GlobalValueKey") = htblGlobalValues

    Application["GlobalValueKey"] can be used anywhere in the whole application by any user. It will be common to all application users.

    Author Comment


    thanks for the quick answer - which is undoubtedly correct for the question I asked. Unfortunately, in my attempt to formulate a simplified question, I simplified a bit too much :-}

    I actually need to instanciate a control such as
    dim MyControl as new MasterController

    Open in new window

    and want MyControl to be globally shared, so that properties can be accessed and modified throughout the application.

    Sorry, my fault. Can you still help me?
    LVL 5

    Expert Comment

    Try to create HashTable with Object as its value
    htblGlobalValues.Add("Key1", Object)

    then  you can get this object and cast it to MasterController where you need in the application.


    Assisted Solution

    Well, I think I have found the (simple) answer to my question now:

    create a class file in App_Code and fill it with this code:

    Public Class UtilsClass
        Public Shared MyGlobalObject As Object 'String or whatever
    End Class

    Open in new window

    then Import this class in each codebehind using

    Imports UtilsClass

    Open in new window


    Author Closing Comment

    Using the Application object is undoubtedly on way to skin this cat - not what I had in mind, but that's due to my incorrect asking. As the answer (that I found myself) is much simpler than the proposed approach, I only award it a "B".

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now