Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public Shared Variable Shared Between Users?

Tags:

asp.net

vb.net

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

like image 419
DotNetDublin Avatar asked Mar 30 '12 08:03

DotNetDublin


People also ask

What are shared methods and what are shared variables?

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.

What is the purpose when you declare the public shared variables in a class file?

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.

What is shared variable in OS?

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.

What is public shared in VB net?

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.


2 Answers

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
like image 102
Curtis Avatar answered Oct 20 '22 21:10

Curtis


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.

like image 4
GregL Avatar answered Oct 20 '22 21:10

GregL