A common shared variable between users?

I took care of the site maintenance (ASP.NET VB) and on one specific page I noticed the code below

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: can the UserNumber variable be accessed or changed by any other user than the current one?

Many thanks

+5
source share
2 answers

A Sharedvariable is the same as a variable staticin C #.

They may be available for all instances, so different users will use the same variable.

If you look at the variable name, I would suggest that you need to delete this Shared


You apply the value of the variable Sessionto Shared Stringon Page_Init.

, , .


, :

Protected UserNumber As String
+9

EDIT . ...

, @Curt, Shared .

, , , , Page_Init.

" ", , UserNumber Page_Init, , , . .

ReadOnly non-shared, :

 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

, "Lazy-load", , .

+4

All Articles