开发者

Will my shared variables loose value? (asp.net vb)

开发者 https://www.devze.com 2022-12-22 21:04 出处:网络
I have a class includes.vb that holds some variables (sharing them with other pages) like: Public Shared pageid As Integer = 0

I have a class includes.vb that holds some variables (sharing them with other pages) like:

Public Shared pageid As Integer = 0

I then have a function that does some work with these variables returning them with values;

Return pageid

When I step through the code, the variables have values (while stepping through the function), but when they are returned to the page开发者_开发知识库, they come back null.

Do they loose value everytime a page is loaded? Can you suggest an alternative method?

Thanks a lot.


You should use probably Session variables.

Session("PageID") = 0

and access it every time you need.

Not a best practice but if you want to be even stricter, you can use specific application variable for every session so that if the user returns after a day to the website, it still not lost (as long as you haven't done an iisreset).

To overcome iisreset will be an even bigger overkill, you can save the value to a file/DB and retrieve it everytime you want. (Please don't do that!!)

Maybe this can explain further: http://codeforeternity.com/blogs/technology/archive/2007/12/19/handling-asp-net-session-variables-efficiently.aspx


It is not a very good idea to use shared variables in web projects: first off, every time you do an "iisreset" or recycle your application pool, these variables are reset. Next thing, these variables are not per-user, but per-process, and (I believe) are not guaranteed to be thread safe, so one thread may change the value of a variable and then another one reset the value to something different.

Judging from the variable name "PageID", I think you are trying to track the last page user has visited. If this is the case, then session variable scope is a better solution for you. See tutorial here: http://msdn.microsoft.com/en-us/library/ms178581.aspx

0

精彩评论

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

关注公众号