Home > OS >  Trying to Wipe Static Variables Every Time the app is closed
Trying to Wipe Static Variables Every Time the app is closed

Time:04-05

Hi I am trying to create a web application with asp.net for a business that involves creating a list of purchases and the total cost of all items, because I need these values to persist between multiple form submissions and need to be able to work across several classes and methods I created them as static variables. The problem that I am having is that even when I completely close the app these values persist into the next time I open it. Is there some way to resolve this issue? What I would like to do is set these variables to a default value every time the user either opens or closes the app.

I have tried a wide variety of options including pre defining the default value of these variables, creating the variables within the page_load method, and nothing seems to work. Please Help.

CodePudding user response:

Sorry, you just cannot go down this road - don't!!!!

If you need some persistence of some class values, then if the class object size is small - not much memory space? Then shove that class into ViewState.

You can also consider using Session(). Which one will depend on your goals.

ViewState: This is per web page (per user).

So, for smaller amounts of information - use Viewstate. And VERY important: ViewState is only per page. However, keep in mind that those "objects" are persisted in the client side browser. While its encrypted and safe? It means that every post-back, button click etc. means those "objects" will be part of your post-backs (they make the round trip from client browser to server, and then travel back to client). but, VERY important is they are per page - not global to to the user.

Session()

session is also great, and is more secure, but MORE important is session() variables and objects are maintained server side in memory. This has a big advantage that your post-back size does not increase. And session() is great for passing values between say two web pages, since they are global to the one user.

However, be careful. Don't write boatloads of code for a given page that uses session WHEN or IF the possibility that two such web pages can be opened by the one user.

Say, we have a grid view - list of houses to buy. So you click on one row, set session() to the pkrow id, and then jump to the next page.

Now, the user opens another tab, or even copy of the browser, goes to the grid view, clicks on a different row, we set session() to that pkrow id, and jump to another page. Now, you have two browser pages open, each sitting on viewing a different house, but the row pk id is the last value you set!!!

This means that clicking on say buy house, and you use session() pk id? Well, one page has the wrong PK id!!!!!

So, keep the above in mind. In fact, for above cases, I adopt a coding standard like this:

Use session() to pass the few values to the next page. (and then you don't have ugly URLs with parameters - and often that is a huge security hole anyway).

Then on target page - first page load if (!IsPostBack),

Transfer the few values to ViewState, and then write ANY code behind for that page based on ViewState values and not session. That way, your software will work correctly EVEN if they open several copies of the browser.

In the above example, then, we passed pk database ID to the next page, but on first page load, we transferred to Viewstate.

Now, it don't matter what that session value is, and they can even launch another copy of the browser, go to the grid and select a row, and a new pk row id is set.

but, our existing page don't care, since it using ViewState to opertate.

So, session() is global to the ONE user, but also global to all web pages. So, some caution as per above is required (don't wind up writing a lot of code on the current page based on session UNLESS you assume that the user will not have more then one copy of that web page open.

But, attempts to use static vars? NO! - don't even try that. You are testing on your developer computer with ONE user, and when you attempt to run more then one user - you have a huge mess on your hands. You can setup some static class values - for use by ALL users hitting the side, but ANY thought of attempting to use static class or vars to persist "user" values is 100% out of the question, and should not be attempted at all.

CodePudding user response:

Thank you for the comment I will try to make use of session() in the future.

  • Related