Saturday, March 24, 2012

maintaining global variable

I am using visual web developer and SQL Express 2005

i am using the membership and roles for user login purposes

i want to maintain a global variable that can be accessed from any page and from any user

please help me

Hi,

you could use the Application object.

It is similar to the Session object but it is accessible for all users.

You could store objects there like that:

int a= 5;

Applicatio["SomeIdentifier"] = a;

int b = (int) Applicatio["SomeIdentifier"];

However, when modifying some variables stored in the application object you need to lock it, since the access to it is not synchronized.

sth like:

lock(Application)

{

// perform the update here

}

Cheers,

Yani


You should use caching:

Dim strTest as String = "Just a test"
Cache.Insert("test", strTest)

And then to access it:


Dim strTest2 as String = Cache.Items("test")


Hope this helps! Please mark the most helpful post(s) as "Answer" for the sake of future readers.


Should not use cache as this can randomly be released by the server if it needs resources.


You can set the duration of the cache.

You should simply declare a global variable (e.g. a static/shared property backed by a static/shared field) within a type held in the App_Code folder (or you could add this to global.asax, if you prefer).

There is no point in using the Cache or Application objects unless you desire the services that they offer (and, personally, I don't think that there's any point in using the Application object whatsoever other than for ease in porting classic ASP apps to ASP.NET). Using a strongly typed global variable is better than using either Cache or Application for the following reasons:

a. You can initialise it correctly to a meaningful value without having to deal with Application_Start.

b. It is strongly typed and is therefore not going to be suffering from boxing/unboxing and other performance issues.

c. You can sensibly wrap validation and synchronisation code within its accessor property's setter.

Where this approach really loses out to Cache is that Cache supports expiration and the expiration callback mechanism. Thus if you wanted to display, say, the top 10 selling products on the site then using the Cache would be a great way to do this, as you could poll the database every hour or so using the timeout mechanism to keep the data semi-fresh/fresh enough. Note that you can also use this technique to perform tasks such as persisting the value of the "global variable" on an occasional basis, if that's of value.

Of course, the concept of a global variable is completely lost in ASP.NET if you use web farming or gardening unless you're prepared to store the data in a backing store. As the perf for this would be lousy, you wouldn't typically bother.

To correct a couple of minor issues with previous posts in this thread

1. The Application objectis synchronised for single read and write operations. You would only need to add synchronisation code for non-atomic operations such as the following:

// assumes Application["Counter"] holds a boxed integer
lock( something other than Application itself, for performance reasons )
{
Application["Counter"] = (int) Application["Counter"] + 1;
}

2. If using the Cache for the purpose of holding a global variable, you're more likely to set the object's CacheItemPriorty to NotRemovable, rather than specify a timeout (unless you wanted to use the timeout techniques mentioned above).

3. Just bear in mind the perf issues when using value types this way. The snippet of C# code above will cause an unboxing and boxing operation to be performed. This is minorly unpleasant. More insidiously, however, after the assignment the previously boxed object will need to be garbage collected. This might be a minor issue, but the more time your code spends in GC the less time your server is handling requests. Of course, when way to overcome this is to write the following:

public class GlobalHolder
{
public int value; // or private with an accessor property
}

and put an instance of GlobalHolder in the Application. But if you're going that far, you might as well simply declare the global variable properly in the first place.

0 comments:

Post a Comment