1. Computing

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

What does it actually mean?

By

Updated June 25, 2007

It's Actually "Shared" and Dynamic Objects

Recently, I was reading the documentation at Microsoft's MSDN site where the author wrote ...

The following example compares two string objects using a "static" method.

Dim MyString As String = "Hello World"
Dim YourString As String = "Hello World"
Console.WriteLine(String.Equals(MyString, YourString))

But the author never defines what a "static method" is. The concept of static and dynamic members of objects is a key idea in both .NET and object oriented programming in general. You just won't understand a lot of VB.NET documentation unless it's completely clear in your mind. The goal of this article is to make it clear.

One of the first things that is wrong with the Microsoft's documentation above is that a "static method" technically doesn't exist VB.NET. That's C# syntax. In VB.NET, it's a Shared member. This is actually an even more serious error because Static is a VB.NET reserved keyword and it means something entirely different. You see this error a lot but you would think that a page at Microsoft's MSDN would get it right.

This is one area in the .NET technology where a quite a few terms that actually mean the same thing are used by different authors. I'll point out the terminology problems as we encounter them in this article.

A Shared method is coded this way:

Public Class Form1
   Private Sub CallSharedMethod_Click( ...
      Result.Text = SharedMethodClass.SharedMethod
   End Sub
End Class
Public Class SharedMethodClass
   Shared Function SharedMethod()
      Return "Returned from the Shared Method"
   End Function
End Class

(Note: In all of the code examples here, the event parameters for the Button components are truncated to keep the statements shorter.)

Here's the code in action:

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

The thing to notice about this code is that it was not necessary to instantiate the Class before using the method in it. (And it has nothing to do with whether it's in the same file or not.) Another way to say this is that shared members belong to the class, not to an instance of the class.

Let's see what happens if we make a change with the goal of changing the class by to a non-shared class by replacing Shared with Public in SharedMethodClass. When you do this, the code won't even compile. This error is generated instead:

Reference to a non-shared (dynamic) member requires an object reference.

To convert this class, you have to instantiate the class before you reference it. Here's the code after it's converted:

Public Class Form1
   Private Sub CallNonSharedMethod_Click( ...
      Dim myDynamObject As New NonSharedMethodClass
      Result.Text = myDynamObject.NonSharedMethod
   End Sub
End Class
Public Class NonSharedMethodClass
   Public Function NonSharedMethod()
      Return "Returned from the Non-Shared Method"
   End Function
End Class

And here's the new code in action:

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

In addition to changing Shared to Public in the called class, the new Dim statement was added to instantiate the object. (The New method does that.) And ... very important ... the instantiated object is used in the next statement instead of the Class name. That is, SharedMethodClass is changed to myDynamObject. From a purely coding perspective, this is the main difference between static (Shared) and dynamic methods. (I changed some of the other names too, but no other changes are required. The changes were just made to make the illustration look better.)

From a design perspective, there are several reasons for a static or Shared method. The first is simplicity and it's illustrated by the Microsoft MSDN example at the very top of the article. If there's no reason for dynamic behavior, it's easier to code:

Console.WriteLine(String.Equals(MyString, YourString))

... than to code ...

Dim myNewStringObj as New String
Console.WriteLine(myNewStringObj.Equals(MyString, YourString))

Besides, VB.NET doesn't include a New method for the String object with no arguments so the first statement above generates another error that you might have seen before:

Overload resolution failed because no accessible 'New' accepts this number of arguments.

You can use one of the three Overloaded New methods for the String object by including the right parameter list. For example, to create a single String object from an array of Char objects, you can use this code:

Dim myCharArray() As Char = {"A", "B", "C"}
'Debug.WriteLine(myCharArray()) ' Doesn't work
Dim myTestString As New String(myCharArray)
Debug.WriteLine(myTestString) ' This works!

©2014 About.com. All rights reserved.