Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable initialized in class loses its previous value with the page loading

Tags:

I've declared a String variable test with "hi". every time I click on Button1, I expect that test will be appended with its previous value. But I have noticed that it loses its previous value when the button is clicked and the page is reloaded. That is every time I click it, it has its text as "hihi". I expect "hihihihi" on the next click and so on. What's the problem here with the code below?

public partial class _Default : System.Web.UI.Page {      String test = "hi";      protected void Page_Load(object sender, EventArgs e)     {      }     protected void Button1_Click(object sender, EventArgs e)     {         test += test;         Button1.Text = test;     } } 
like image 463
Md. Arafat Al Mahmud Avatar asked May 31 '12 17:05

Md. Arafat Al Mahmud


People also ask

Which variable does not retain its value?

Static variable NOT retaining its value, but Instance variable is.

Do static variables get initialized?

A static variable in a block is initialized only one time, prior to program execution, whereas an auto variable that has an initializer is initialized every time it comes into existence. A static object of class type will use the default constructor if you do not initialize it.

Why static variables are initialized to zero?

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code. These variables are allocated in .

Which variables are automatically initialized to zero?

3) Static variables (like global variables) are initialized as 0 if not initialized explicitly.


1 Answers

No, that's not the way asp.net works. If you need that behavior you should do this:

public string test {   get {     return (string) ViewState["test"] ?? "hi";   }   set {     ViewState["test"] = value;   } } 

When ASP.NET sends a request to the server, a new version of your class is instantiated. If you need to get the state, you need to use ViewState (which is saved in a hidden field in the browser and sent with every request, and therefore state saved per page), or you can use SessionState which is a state saved per user. SessionState by default is saved in memory. So, if you restart IIS, this state will go away. Note that viewstate's state will NOT go away if you reset IIS (since it's being sent by the browser). You can also use the Cache which again, is saved in memory. This state is for all users of your application. The same rules about resetting IIS apply. Finally, you could make your variable static. As I said, every time a request is made a new version of your class is instantiated. Of course, static variables are not instance variables, so the state of a static variable is saved across postbacks as well. The same rules about IIS reset apply to static variables as Cache and Session.

like image 104
aquinas Avatar answered Oct 08 '22 12:10

aquinas