VB Shared variable 與 Application

VB Shared variable 與 Application

昨天有測試過 c# Static 是不是跟 Application有一樣的效果??
今天又來測試 vb Shared 是不是跟 Application有一樣的效果??
於是又進行了測試,從測試結果中可以發現
即使從 不同 Client 端的瀏覽器Shared 變數仍然是累加上去的

SharedClass.vb


Imports System.Web.HttpContext
Namespace Shared_NS
    Public Class SharedClass

        Private _name As String
        Private _age As Integer
        Private Shared _increm As Integer

        ' 建構子
        Public Sub New()
        End Sub

        ' 帶參數建構子
        Public Sub New(ByVal name As String, ByVal age As Integer)
            _name = name
            _age = age
        End Sub

        ' Name 屬性
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal Value As String)
                _name = Value
            End Set
        End Property

        ' Age 屬性
        Public Property Age() As Integer
            Get
                Return _age
            End Get
            Set(ByVal Value As Integer)
                _age = Value
            End Set
        End Property

        ' Say Hello
        Public Sub SayHello()
            Dim txtHello As String = String.Format("Hello ~ My Name is {0}. <br> I am {1} years old. ", _name, _age)
            Current.Response.Write(txtHello)
        End Sub

        ' _increm累加1,並顯示
        Public Shared Sub Exec_Display_Increm()
            _increm += 1
            Current.Response.Write("<br>---------------------------------<br>")
            Current.Response.Write(String.Format("_increm:{0}", _increm))
        End Sub

    End Class
End Namespace

WebForm1.aspx


Imports vb_1041112.Shared_NS

Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form 設計工具產生的程式碼 "

    '此為 Web Form 設計工具所需的呼叫。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    '注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
    '請勿刪除或移動它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
        '請勿使用程式碼編輯器進行修改。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' 建立實例物件
        Dim SC As New SharedClass("Tzu-Chun", 33)
        SC.Name = "蛋黃哥"
        SC.Age = 18
        SC.SayHello()

        ' 調用靜態方法(shared _increm 累加1)
        SharedClass.Exec_Display_Increm()

    End Sub

End Class

image

 

image

image