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

"Static" (that is, "Shared") versus Dynamic Types in VB.NET
Dynamic Object Members

By , About.com Guide

Jun 25 2007

The opposite of static or Shared is dynamic and many of you will think of this as the "normal" way to declare and use objects. To illustrate the difference, let's code the same functionality of the example above with dynamic variables. Here's one way to do it.

Public Class Form1
   Dim myDynamClass As NonSharedMethodClass _
      = New NonSharedMethodClass
   Private Sub FirstCallSharedMethod_Click( ...
      PrintResult(myDynamClass.NonSharedMethod)
   End Sub
   Private Sub SecondSharedMethodCall_Click( ...
      PrintResult(myDynamClass.NonSharedMethod)
   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 NonSharedMethodClass
   Private m_CallCount As Integer = 0
   Public Function NonSharedMethod()
      m_CallCount += 1
      Return m_CallCount
   End Function
End Class

Notice that an instance of the class, myDynamClass must instantiated using New and must also be global to the Form1 class. (That means it's declared at the Class level so all the members in that class can access it.) The class creates a Private variable, m_CallCount, that is updated every time the class is called.

(Extra credit homework question: Suppose the NonSharedMethodClass was instantiated inside both the FirstCallSharedMethod_Click and the SecondSharedMethodCall_Click subroutines. What other changes would have to be made to get the same result. Hint: More than one change is required.)

This is slightly "off topic" but if you play around with the code (and I hope you do), you will discover that even when you declare a class member as Shared, VB.NET will still allow you to code it as if it was dynamic.

Dim mySharedClass As SharedMethodClass = New SharedMethodClass
PrintResult(mySharedClass.SharedMethod)

[... and then in mySharedClass ...]

Public Class SharedMethodClass
   Shared Function SharedMethod()

The compiler flags it as an error and even, helpfully, tells you how to correct the error. (See the illustration below.)

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

But the code still runs! The compiler forces it to run by making the suggested change for you! To me, this is a throwback to Visual Basic 6 where the compiler would make all sorts of assumptions about what the programmer "meant to code" and caused all sorts of horrible problems in production. (Variant datatype, anyone?) In a big program, this could easily be overlooked. They should never have allowed it.

This example introduces another concept of shared members: instance variables (also called member variables). The variable m_CallCount is called an instance variable because a new copy (instance) is created every time the object is instantiated. Although the example introduces the topic, it's not really good for explaining it because we only create one copy.

Since this is the "normal" way, and both books and the web are full of examples (such as my Visual Basic .NET 2005 Express Tutorial), I'll just code a fairly standard sample of how two instances of an object are used. In this example, I'll create two Customer objects with different Name properties and display them both together. If you were reading accounts from a database, this kind of requirement might pop up quite often.

Public Class Form1
   Private Sub DisplayCust_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayCust.Click
      Dim newCust1 As New Customer
      newCust1.Name = "George Washington"
      PrintResult(newCust1.Name)
      Dim newCust2 As New Customer
      newCust2.Name = "Thomas Jefferson"
      PrintResult(newCust2.Name)
   End Sub
   Public Sub PrintResult(ByVal Name As String)
      Result.Text += Name & vbCrLf
   End Sub
End Class
Public Class Customer
   Dim m_Name As String
   Property Name() As String
      Get
         Return m_Name
      End Get
      Set(ByVal value As String)
         m_Name = value
      End Set
   End Property
End Class

And ... the code executing:

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

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. 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.