简体   繁体   中英

Weird Behaviour of Public Shared Variable in Vb.net

I have a web application written in vb.net where i made use of public shared variable to persist the value of the variable on each post back to the server. I noticed a weird situation in my application.Let me explain by the following example:

  Public Shared totalScore as integer
  Private Sub CalculateTotal()
    method body..........
  totalScore = 45 (assuming the student gets 45)
  'totalScore value is stored in the database'
  after the processing i set totalScore back to 0
  end Sub

This is where the problem is after setting the totalScore to 0. I noticed that for the first user of the web application everything is fine but for subsequent users running this subroutine totalScore is always 0.

No matter what their score is. Please explain to me Public Shared Variables and their effect on an application.

Have tried reading it up on Microsoft unorganized bible site but can wrap my head round what they are trying to say. Explanation is highly needed or what is the best way to persists data between postbacks.

Your variable is recreated every time you make an action in the browser and your code is loaded on the server. Between each trip to the web server, there should be the least state kept in other to provide scalability.

If you need to keep state, you will need to decide if it is session (just for one user) or application. Check this article to get the details on how to do it: http://www.c-sharpcorner.com/UploadFile/78d182/Asp-Net-state-management-techniques/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM