0

When are Shared (Static) variables created and destroyed. For example have a look at the code below:

 Imports System.Data.SqlClient
 Public Class Form1

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Person.testCon = New SqlConnection
            Person.Age = 30
            Dim p1 As Person
        End Sub
    End Class

Imports System.Data.SqlClient
    Public Class Person
        Public Shared testCon As SQLConnection
    End Class

The testCon variable is accessible from the Form_Load before the first instance of Person is created. I realise that it is probably not good practice to have a connection as a shared variable but I want to get my point across. I also want to know when variables are created and destroyed if they are primitives (like Person.Age in the example)

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • 1
    Don't use static/shared connections http://stackoverflow.com/a/9707060/284240 – Tim Schmelter Jul 21 '12 at 15:43
  • 1
    @TimSchmelter, I specifically said in the question: " I realise that it is probably not good practice to have a connection as a shared variable but I want to get my point across". However, the link is very interesting +1. – w0051977 Jul 21 '12 at 15:49
  • 1
    You have tagged your question as asp.net, but it seems a winforms application. Could you clarify? – Steve Jul 21 '12 at 15:54
  • @Steve, the question is relevant to VB.NET and ASP.NET. The example is VB.NET. – w0051977 Jul 21 '12 at 17:37

1 Answers1

0

Shared variables live for the length of your application, according to Microsoft.

However, object type variables are only instantiated when you tell them to be.

You can verify this behavior by creating a new test class:

Public Class Class1
    Sub New()
        System.Diagnostics.Debug.Fail("Class Created")
    End Sub
End Class

Then create a shared variable for this class as a member variable in another class:

Private Shared m_TestClass As Class1

If you don't access the shared variable, the Debug.Fail statement will not be executed. However, as soon you instantiate this class and assign it, it will be fired (just like any other object):

    m_TestClass = New Class1

Shared variables live until the AppDomain they reside in is unloaded.

You could also test this by adding a Finalize statement to the test class with a similar Debug.Fail statement.

The lifetime is one reason that you should use SyncLock when assigning new values to object-type shared variables.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • Do you know when the reference to the shared object (SQLConnection in this case) is created? – w0051977 Jul 21 '12 at 19:11
  • @w0051977: Yes, in your Form1_Load event. See my modified comments (shared variables are not instantiated until you tell them to be). – competent_tech Jul 21 '12 at 19:18
  • Thanks. I realise that these shared variables (objects) are not instaniated until you want them to be and you can set them to Nothing at some point if you want to. When is the reference created? – w0051977 Jul 21 '12 at 20:26
  • The reference is created the first time the class is explicitly created or when the shared instance is implicitly created by accessing a shared member (field, method, or property). – competent_tech Jul 21 '12 at 20:59
  • Line 1 of the form_load shows an instance being created when there is no reference. Does this mean that the reference was created by: "accessing a shared member (field, method, or property)". – w0051977 Jul 21 '12 at 21:33
  • Yes, that is my understanding. – competent_tech Jul 21 '12 at 21:39
  • The shared variables are still in scope when all the references to person are nothing. Therefore I am still unsure when they go out of scope. It must be as you say; at the form unload? – w0051977 Jul 21 '12 at 21:47
  • Sorry, shared variables do not go out of scope until the AppDomain is unloaded (i.e. your program is shutdown). – competent_tech Jul 21 '12 at 21:55
  • I realise that the ASP.NET AppDomain is unloaded when the W3WC process is recycled (correct me if I am wrong). When is a VB.NET client application unloaded (when the form_unload event is called perhaps). This is my last question. I will marked the question as answered when I get your response. – w0051977 Jul 22 '12 at 19:51
  • In most cases a winforms application is only unloaded when the exe the user launches terminates. I was careful to mention AppDomain earlier because you can actually have an exe startup, then load another exe into a new AppDomain. We use this mechanism to enable self-updating applications. If you don't unload the AppDomain of the second executable when the user is done with it, the shared variables remain in effect until the first exe terminates. – competent_tech Jul 22 '12 at 22:41