Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Static Variable 1

Status
Not open for further replies.

MarkZK

Technical User
Jul 13, 2006
202
GB
Hi All,

I read somewhere that the "Application Object" was only added to ASP.NET to help users coming in from a Classic ASP background.

If that is true, how do you create a dynamically named variable that is globally accessible to all users ?

For example with Application it could be...

Application("app_" & i) = someVar

Is that possible with Global Vars ?...

Code:
Public Class TheStaticClass

	Shared TheGlobalVar As String 'can this variable name be created dynamically ?
	Shared applock As New Object()

	Public Shared Property TheApplicationProperty() As String 'ideally needs to pass the created variable name in this method
		Get
			SyncLock applock
				Return TheGlobalVar '<-- dynamic name ?
			End SyncLock
		End Get
		Set
			SyncLock applock
				TheGlobalVar = value '<-- dynamic name ?
			End SyncLock
		End Set
	End Property
End Class

... Or is Application the way to go ?

Thanks
 
how do you create a dynamically named variable that is globally accessible to all users
Code:
Application[key] = value;
the applicationstate object manages locking internally.however, the question is why would you do this? typically you want to avoid globally static variables as much as possible. In fact the only time you want them is when the object must be a singleton. but even then there are much better ways of managing object scope.

so is it possible with asp.net yes. Is it advisable, absolutely not. At a minimum you can create readonly static fields/properties to control these objects. I like to use IOC containers in my projects and configure the object scope via the container.

also, know that the application scope is shared across all users for the life of the application from the very first request to the very last response. for user specific values you can store objects in the session. But even this should be managed wisely. Don't just start throwing objects into session because it's available.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Hey Jason,

Thanks for the reply, This is the page that I read (the part about performance increase).

I figured it'd be best to just use the Application, but that page made me think that they had in some way made it obsolete or unnecessary in ASP.NET now, but I guess not if "Static members" don't offer everything that Application does.

Unfortunately my host would not set my Application Pooling delay time-out to 24 hours like I wanted, so this may all be in vain as I'm likely going to store data into a database in stead (no doubt a performance decrease).

Thanks again!
 
while preformance is important, it's not everything. first get it to work, then refactor the code so it's maintainable, then improve performance.

Usually the biggest performance improvements come from reducing remote calls, and limiting the amount of data returned from the remote call(s). whether you stored the data in a static field or the application object is indifferent.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top