0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Joe,Thanks.Using Shared solved my problem. Is the value of rc different between different user sessions running simultaneously?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.