Object-Oriented Changes for Modules in Visual Basic

Shared members are class members, such as properties and procedures, that are available for all the instances of a class, but not associated with any particular instance of a class. For more information on shared members, see Shared Members in Visual Basic.

Instance Data and Shared Data

Instance data is the set of data created when the New keyword is executed. By contrast, the shared data of a class is created independently of any execution of the New keyword.

Visual Basic 6.0

Visual Basic 6.0 does not support shared data in class definitions. To share data across class instances, a global variable in a module is used.

Visual Basic 2008

Shared data is declared by adding the Shared (Visual Basic) modifier to the variable declaration statement, as shown.

PrivateShared totalInstances AsInteger

Shared Methods

Visual Basic 6.0

Visual Basic 6.0 does not support shared class members. Similar functionality is available from modules. The runtime only creates one instance of the data in a module. The data and methods of a module have a project-wide scope. Here is an example of a library implemented in a module:

Public Sub GetTitle(wordXml As String)
    ' Add code here to find title in Xml string.
End Sub

Public Sub ReplaceTitle(wordXml As String, newTitle As String)
    ' Add code here to replace title.
End Sub

Visual Basic 2008

A more object-oriented approach to modules is to use shared class members, if the members are related to each other as a library, or if the members are related to an existing class. The design will determine if all the members are shared, or whether a class has a mix of shared and instance members. If the class has only shared members, adding only a private constructor will prevent the class from being instantiated by client code. The following example shows a class that cannot be instantiated by client code and the client code that calls one of the members.

PublicClass WordLibrary
    PublicSharedSub GetTitle(ByVal wordXml AsString)
        ' Add code here to find title in Xml string.EndSubPublicSharedSub ReplaceTitle(ByVal wordXml AsString, _
        ByVal newTitle AsString)
        ' Add code here to replace title.EndSubPrivateSubNew()
        ' This prevents instantiation of the class in the client.EndSubEndClass
' Code that calls the library method.Sub ChangeTheTitle()
    WordLibrary.ReplaceTitle("Old Title", "New Title")
EndSub

Some advantages of using a class over a module for this library are:

  • The library has a name, WordLibrary, that is explicitly called out in the code.

  • The methods are grouped into one class, indicating their relationship to each other.

  • The use of a private constructor makes it clear that this class provides a set of utility methods.

  • The client code uses the library name, making the project organization clear to the reader.

In the .NET Framework, shared methods are commonly used to provide functionality that is related to a class, but does not require an instance of the class. One example is the Parse method that is supported by many of the data types in the .NET Framework. The Integer Data Type (Visual Basic) supports a Parse method that takes a string as parameter. The Parse method returns the integer represented by the string. It does not make sense for the Parse method to be a member method. There is no integer value to manipulate, only a string.

Accessing Shared Members

Visual Basic 6.0

Visual Basic 6.0 does not support shared class members.

Visual Basic 2008

Shared members can be accessed using the class name or instance name, as shown.

WordLibrary.ReplaceTitle("Old Title", "New Title")

Shared methods cannot access any instance data unless a reference to an instance of the class is passed to the shared method.

Upgrade Suggestions

The Visual Basic 6.0 WordLibrary module is upgraded to the following code:

Module WordLibrary
    PublicSub GetTitle(ByRef wordXml AsString)
        ' Add code here to find title in Xml string.EndSubPublicSub ReplaceTitle(ByRef wordXml AsString, _
        ByRef newTitle AsString)
        ' Add code here to replace title.EndSubEndModule

When upgrading module code from Visual Basic 6.0 to Visual Basic 2008, consider the following:

  • Organizing module methods and data into classes as shared data and methods.

  • Moving module methods into existing classes as shared methods, when the method is related to the class.

See Also

Concepts

Shared Members in Visual Basic