1. Home
  2. Computing & Technology
  3. Visual Basic

"Static" (that is, "Shared") versus Dynamic Types in VB.NET
Coding a Shared Class Member

By Dan Mabbutt, About.com

Jun 25 2007

The second reason for using a Shared member is to ... well ... "share" it. Consider this code that counts the number of times a shared method is called. The variable m_SharedVar keeps it's value because there's only one copy of it.

Public Class Form1
   Private Sub FirstCallSharedMethod_Click( ...
      PrintResult(SharedMethodClass.SharedMethod)
   End Sub
   Private Sub SecondSharedMethodCall_Click( ...
      PrintResult(SharedMethodClass.SharedMethod)
   End Sub
   Friend Sub PrintResult(ByRef CurrentCallCount As Integer)
      Result.Text += "The shared class has been called " _
         & CurrentCallCount.ToString & _
         " Time(s)" & vbCrLf
   End Sub
End Class
Public Class SharedMethodClass
   Shared m_SharedVar As Integer = 0
   Shared Function SharedMethod()
      m_SharedVar += 1
      Return m_SharedVar
   End Function
End Class

And again, a snapshot of the code being executed is in the illustration below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Notice that the variable m_SharedVar is also declared as Shared.

The default declaration of .NET objects is non-shared (dynamic). If you try to declare it as a non-shared variable (with the Dim keyword), you have to move it inside the Function block and, because it's non-shared, it's initialized every time the function is called. So the function always returns the value 1. It's worth keeping in mind that Shared isn't the same thing as Public. The m_SharedVar variable is still Private to the object so it can't be altered by code outside the class. (You can add Private to the declaration and it doesn't change a thing.)

Another variation on this pattern is called a shared constructor. If you code a Shared New method, called a shared constructor, in a class ...

Public Class Form1
   Private Sub Form1_Load( ...
      Debug.WriteLine(SharedConstructorClass.sharedField)
   End Sub
End Class

Class SharedConstructorClass
   Public Shared sharedField As String
   ' shared constructor
   Shared Sub New()
      sharedField = "Initialized by CLR!!"
   End Sub
End Class

... then when the class is used, the constructor is called automatically by the common language runtime (CLR). The code above displays "Initialized by CLR!!" in the Immediate window.

You can use the Shared keyword with

  • Dim
  • Event
  • Function
  • Operator
  • Property
  • Sub

On the next page, we switch from Shared to dynamic.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. "Static" (that is, "Shared") versus Dynamic Types in VB.NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.