开发者

Does public variable scope is for all sessions, or each session has its variable copy?

If i declared a variable as public in a web page, does this variable can hold the same value between different sessions, or each session has its own copy?

And if yes it holds between sessions, how to prevent that? is there an attribute to declare as for one session copy?

May be its a silly question? but i am confuse开发者_StackOverflow中文版d about something..


The variable is only available to the single request unless you store it in the Session or Cache. If it is public it will be available to other objects created during the request, but only to those object associated with the request. If you declare it to be static as well, then it will be available for the lifetime of the application -- but this sounds like what you don't want, so simply avoid doing it.


Variable scope doesn't have anything to do with values being held between sessions.

public scope just means that the variable can be seen by any other code in your application.

If you want the object to be able to be stored across sessions you need to store it in the Session.


  1. Every Page is an instance of the WebPage class, which is destroyed and recreated on every request.
  2. The session bag is unique to the session and is not shared across different sessions. It only stores stuff you add to it.

1 + 2 = No. Just adding a public field to a page does not add it to session. The same value does not hold between sessions.

Can you tell us the exact issue you are facing?


The only way to share data between sessions is by storing it in a database or in application variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜