Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-10-27
5
Medium Priority
?
467 Views
Last Modified: 2012-05-12
Hi ASP.net (VB) experts,

for my ASP.net (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,
Thomas
0
Comment
Question by:Staudte
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
mcs0506 earned 2000 total points
ID: 37037158
Hi

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)
Else
      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.
0
 

Author Comment

by:Staudte
ID: 37037206
Hi,

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?
0
 
LVL 5

Expert Comment

by:mcs0506
ID: 37037257
Hi,
Try to create HashTable with Object as its value
e.g.
htblGlobalValues.Add("Key1", Object)

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


Dani
0
 

Assisted Solution

by:Staudte
Staudte earned 0 total points
ID: 37052713
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

0
 

Author Closing Comment

by:Staudte
ID: 37081628
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".
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Screencast - Getting to Know the Pipeline
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

578 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