This documentation is archived and is not being maintained.

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 2005

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

Private Shared totalInstances As Integer

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 2005

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.

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

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

    Private Sub New()
        ' This prevents instantiation of the class in the client.
    End Sub
End Class

' Code that calls the library method.
Sub ChangeTheTitle()
    WordLibrary.ReplaceTitle("Old Title", "New Title")
End Sub

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 2005

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
    Public Sub GetTitle(ByRef wordXml As String)
        ' Add code here to find title in Xml string.
    End Sub

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

When upgrading module code from Visual Basic 6.0 to Visual Basic 2005, 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

Show: