I've taken over the maintenance of the website (ASP.NET VB) and on one particular page I noticed the below code
Inherits System.Web.UI.Page
Public Shared UserNumber As String
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
UserNumber = Session("UserNumber")
...
End Sub
My question is whether the variable UserNumber can be accessed or changed by any other user than the current one?
Many thanks
A shared variable or event is stored in memory only once, no matter how many or few instances you create of its class or structure. Similarly, a shared procedure or property holds only one set of local variables. Accessing through an Instance Variable.
You can declare a member variable so that you can access it from anywhere in your code without primarily declaring a variable of the class. In order to have such a member variable, you must explicitly create it as shared.
Shared variables are used to save the state, either at the module level or for the duration of a job. Using shared variables, you can share data across process instances associated with a module or a job. A process instance can read or update the data stored in a shared variable.
The "Public Shared Function " modifier means that you can access a function without a class instance. Any non-shared members have to be called off of a instance of the class.
A Shared
variable is the same as a static
variable in C#.
These can be shared across all instances, therefore different users would share the same variable.
Looking at the variable name, I would assume you would need to remove this Shared
You are applying a Session
variable value to the Shared String
on Page_Init
.
Therefore each time the user loads the page, their session variable will override the current value.
If you are not using this variable outside of this class, then I would recommend changing it to:
Protected UserNumber As String
EDIT My initial answer was incorrect. Trying again...
Technically, as @Curt indicated, a Shared
variable is shared across instances of the class.
However, with the code as is, it is less likely that the value will be shared amongst users as it is set to each user's local copy of the value in their Session in Page_Init
.
There is a possible "race condition" where after the shared variable UserNumber
has been initialised in Page_Init
, and another user submits a request which updates the value of that variable from their session, the first user will then see the second user's value. i.e. users can see other user's values for concurrent requests.
Instead, I recommend using a ReadOnly
non-shared property to get the value from the Session once:
Private mUserNumber As String
Public ReadOnly Property UserNumber As String
Get
If String.IsNullOrEmpty(mUserNumber) Then
mUserNumber = Session("UserNumber")
End If
Return mUserNumber
End Get
End Property
This uses a pattern called "Lazy-loading", which I use in read-only properties a lot on pages to improve performance and readability of code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With