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

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,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StaudteAuthor Commented:

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?
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.

StaudteAuthor Commented:
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

StaudteAuthor Commented:
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".
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.