Global variables in .Net with multi-users are getting switched.

We have a .Net web application. Due to many reasons, I created a Public Class in App_Code folder. It has, among others:
Private Shared _AltPopNum As Integer

  Public Shared Property AltPopNum() As Integer
      Return _AltPopNum
    End Get
    Set(value As Integer)
      _AltPopNum = value
    End Set
  End Property

Open in new window

Apparently, everything worked fine. A couple pages set this value, which can then be accessed in another couple pages. But recently, a case occured where this value was switched between users. That is, UserA inserted the value 10. Two seconds later, UserB inserted the value 15. When UserA read the variable, it was assigned as 15.

Now, I was under the impression that global class variables were stored pretty much like session variables, that is, they are linked to the username/computer. A user in one computer would have totally separate variables from those of another user in another computer. I decided to use this approach because these are short term variables (usually used within the next minute or two and then become useless) and I already had the class created for other methods. But if this isn't reliable, then I'd have to switch to keeping these values in a table and connecting to the database all the time, which I'd rather not do. Session variables would share the same problems, despite being shorter lived (we've tried to make them stay alive for more than 20 minutes, but utterly failed, despite trying various different solutions).

So, does anyone know why this is happening? Is this to be expected from using global variables? Or is this just some freak problem? Can it be solved via some settings, on .Net or IIS? Will I have to move these variables to a table in the DB? What should be the best approach?

EDIT: It should also be noted that this particular user kept having wrong values returned for some time (to my knowledge no other changes occured within seconds of it), then changed computer and, apparently, things worked fine.
LVL 18
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Because of the very nature of shared variables, they will be created one in an AppDomain and will remain till the AppDomain Unloads, so its always advisable to synchronize the access to shared variables if they are intended to by used by many threads at one point in time.
This can be done using Monitor.Enter and Monitor.Exit Methods or SyncLock VB Statement

Here is a tutorial to help you on that:-
CluskittAuthor Commented:
Does that apply to a web application? There aren't multiple threads, just multiple sessions on different computers. Maybe I'm just being a bit thick (I'm fairly decent at desktop programming, but I hate web dev), but it seems to me that it applies only to a desktop app?
yes it does apply to web, as each request i handled in separate thread....
Just synchronize the shared variable and see the effect....

Read here:-

Summary : Either get rid of them(Preferred) or synchronize them
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

CluskittAuthor Commented:
Ok. This, however, seems to apply for multiple instances on the same computer. Which makes sense, because it would share the same session/cookies/etc. But my problem happened with two different computers, on two different locations. Shouldn't those be handled separately? Or does the ASP.Net engine simply consider any connection as a thread?

I could fix this easily with url redirection, but I don't wish to have any data on the URL where it can be manipulated.

Also, it seems to me that the purpose of the first link you provided is to prevent access to variables while they're being written. And that isn't the problem I'm having. The problem I have is that, using my above example, UserA should have a value of 10, even though UserB has a value of 15 for the same variable. In fact, UserA should have a value of 10 even if 50 different users assign a value to their variable. Each variable should be user independent.
To Make Each Variable User Independent and maintain state, it has to be an instance variable and you have to manage state by saving the instance of the type in cache or session.

class MyClass
private int myValue;

public MyClass(int value)

MyClass obj=new MyClass(10);

Open in new window

If you keep on using Shared Variables, they will be shared in many instances of same type in different threads as each request is handled separately by IIS.

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
CluskittAuthor Commented:
So, the only solution for keeping variables User Independent without using session (or url redirection), would be DB based, right? Luckily, we don't use those for that many pages, so I won't have to replace much code, but we do use those pages quite a bit. I was hoping for a way to store them locally without session variables, due to the difficulties in maintaining their persistence.
another way is to store them on client in some hidden text boxes...
If your data is relatively lesser, then try for this approach...
CluskittAuthor Commented:
It is lesser, but it has to carry across pages, which is why I used the global variables route. Basically, half a dozen variables are set in one page, which then have to be accessed by another different page, which uses those variables for some parametrization.
use cookies then....
CluskittAuthor Commented:
Those are pretty much the same as session variables and can be tampered with like url redirection. I guess I will have to use DB based variables. It will be a small table anyway. Even if we have about 100 users, we won't have more than 2000 records at any time. With the added bonus that variables will carry over different sessions, which might actually have its uses. Thanks for your help. I'm going to close the question now.
CluskittAuthor Commented:
Thanks for the help.
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
.NET Programming

From novice to tech pro — start learning today.