By Microsoft’s definition, Shared.: “Specifies that one or more declared programming elements are associated with a class or structure at large, and not with a specific instance of the class or structure.” Source
What does this mean?
If you have a class and within that class a function is declared as shared then to access that function the class does not have to be created (instantiated) in order to execute the function.
Public Class Incrementer ''' <summary> ''' A shared property. ''' </summary> ''' <value> ''' The shared value. ''' </value> Public Shared Property SharedValue As Integer ''' <summary> ''' A non shared property value. ''' </summary> ''' <value> ''' The non shared value. ''' </value> Public Property NonSharedValue As Integer ''' <summary> ''' Shared: increment value (no local storage). ''' </summary> ''' <param name="value">The value.</param> ''' <returns></returns> Public Shared Function IncrementShared(value As String) As Integer Return CInt(value) + 1 End Function ''' <summary> ''' Non-Shared: Increment value (no local storage). ''' </summary> ''' <param name="value">The value.</param> ''' <returns></returns> Public Function IncrementNonShared(value As String) As Integer Return CInt(value) + 1 End Function ''' <summary> ''' Shared: Increments the static variable. ''' </summary> ''' <returns></returns> Public Shared Function IncrementSharedStatic() As Integer Static value As Integer value += 1 Return value End Function ''' <summary> ''' Non-Shared: Increments local static variable. ''' </summary> ''' <returns></returns> Public Function IncrementNonSharedStatic() As Integer Static value As Integer value += 1 Return value End Function ''' <summary> ''' Shared: Increments the class (local) property. ''' </summary> ''' <returns></returns> Public Shared Function IncrementSharedProperty() As Integer SharedValue += 1 Return SharedValue End Function ''' <summary> ''' Non-Shared: Increments the class (local) property. ''' </summary> ''' <returns></returns> Public Function IncrementNonSharedProperty() As Integer NonSharedValue += 1 Return NonSharedValue End Function End Class
Usage:
Shared |
Non-Shared |
Incrementer.IncrementSharedStatic.ToString
|
Dim inc As New Incrementer inc.IncrementNonSharedStatic.ToString
Note: in order to execute the shared methods you have to use the same syntax as on the left. |
How does this affect my code?
In a multi-user application, for example, a web application having shared or static variables will affect to the scope of the variables have access to. Here is a sample application that uses the Incrementer class:
Iteration |
Session A (IE) |
Session B (FireFox) |
Session C (Chrome) |
1 |
|||
2 |
You will notice that both the Static Variable Non-Shared Counter and Property Non-Shared Counter do not change, since this is an ASP.Net sample and no data is stored in the session, the internal counters are reset to 0 on postback. The Shared Static and Property was incremented across all sessions, why? “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.” Source
Does having shared functions mean that all users are using the same variables declared in the shared function?
Only if a variable is static or property is declared as shared then all users will be accessing the same memory (same value).
Private inc As New Incrementer Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load lblSharedCounter.Text = Incrementer.ExecuteShared() lblNonSharedCounter.Text = inc.ExecuteNonShared() End Sub
''' <summary> ''' Executes the shared function. ''' </summary> Public Shared Function ExecuteShared() As String Dim localValue As String If String.IsNullOrEmpty(localValue) Then localValue = Guid.NewGuid.ToString End If Return localValue End Function ''' <summary> ''' Executes the non-shared function. ''' </summary> Public Function ExecuteNonShared() As String Dim localValue As String If String.IsNullOrEmpty(localValue) Then localValue = Guid.NewGuid.ToString End If Return localValue End Function
If the shared function was using the same data, I would expect my results to show the same value across all sessions. The variables are not declared shared so they do occupy their own memory space.
Private Shared localValue As String ''' <summary> ''' Executes the shared function. ''' </summary> Public Shared Function ExecuteShared() As String If String.IsNullOrEmpty(localValue) Then localValue = Guid.NewGuid.ToString End If Return localValue End Function