I am using Vb.net.
I have a counter variable .
Dim rc as Integer.
This is a class level variable.
On loading the page for the 1st time, its value should be 0.
But later I manipulate its value in various methods.
My page reloads after most methods & the value of rc is re-initialized to 0.
Please suggest how can I avoid this. I need the page to reload but the counter should keep incrementing.
Add a comment
|
1 Answer
You'd need a "Shared" (a.k.a. static) variable:
Public Shared rc As Integer
This will keep your value in memory as long as the application is alive. When the application is reset (for any number of reasons), you'll lose the value.
If you need to keep it longer than that, then you'll need something more persistent, like storing the value in a database.
3 Comments
krum_cho
Joe,Thanks.Using Shared solved my problem. Is the value of rc different between different user sessions running simultaneously?
Joe Enos
It's attached to the type of the page, so it knows nothing about ASP.NET, users, sessions, or anything else including even the instances of the page - you can put this variable in your Global class, or in any other normal .NET class. As long as the application is running, there's only one value. If you need it to know about users, you'd need to use the Session object, or tie it together somewhere else.
Joe Enos
You may want to take some time to get an introduction to object oriented programming - it's very important to distinguish static/shared variables from instance variables, among other things. It would be worth your time to look into it.