How to share data across multiple instances of a class without using a module in Visual Basic 2005 or in Visual Basic .NET (311319)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q311319
For a Microsoft Visual C# 2005 and Visual C# .NET version of this article, see 815778.

SUMMARY

This article describes how to share data across multiple instances of a class without using a module.

The need to share data among components in a Microsoft Visual Basic application has long been addressed by using Public variables in modules. In previous versions of Visual Basic, modules are considered "Global," but are in fact "Global" only in that thread. By using the Public keyword, the variables, subs, and functions that they contain are available to all objects in the application.

Microsoft Visual Basic 6.0 uses Microsoft ActiveX .exe files (COM servers), in which you might want to share data across multiple instances of a class. The solution is to place a variable in a BAS module. There is a better solution in Microsoft Visual Basic 2005 and in Microsoft Visual Basic .NET, which is to use Shared data members. You do not have to use modules to accomplish this.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • General familiarity with Visual Basic 2005 or Visual Basic .NET
  • General familiarity with Visual Studio 2005 or Visual Studio .NET

Additional information

Each class instance maintains its own data. The data is isolated in such a way that if a data member changes in one instance of the class, the same data member in another instance of the class is unaffected. However, the shared members of a class provide a way for all instances of the class to use the same memory location or function.

In Visual Basic 2005 or in Visual Basic .NET, you can still write applications that use modules and public variables to share data among multiple instances of a class. However, using shared data members is a better solution.

If you prefix one of your data members with the Shared keyword, Visual Basic 2005 or Visual Basic .NET creates a single location for the shared data member. The data member is exposed to each instance of the class that references the storage location. If you change the value of the shared data member in one class instance, it is changed in all of the other instances.

Step-by-step example

  1. Start Visual Studio 2005 or Visual Studio .NET.
  2. Create a new Console application by using Visual Basic 2005 or Visual Basic .NET.
  3. Delete all of the existing code in Module1.vb, and then paste the following lines in the module.
    Public Class ClsDemo
        Public Shared MySharedData As Integer
        Private Shared MyLocalData As Integer
    
        Public Shared Property MyMember() As Integer
            Get
                MyMember = MyLocalData
            End Get
            Set(ByVal Value As Integer)
                MyLocalData = Value
            End Set
        End Property
    End Class
    
    Public Class Test
        
        Shared Sub Main()
            Dim x As ClsDemo = New ClsDemo()
            Dim y As ClsDemo = New ClsDemo()
    
            x.MySharedData = 100
            Console.WriteLine("Shared Data: " & y.MySharedData.ToString)
    
            x.MyMember = 200
            Console.WriteLine("Shared Data: " & y.MyMember.ToString)
            Console.WriteLine("Press the Enter key...")
            Console.ReadLine()
        End Sub
    End Class
    					
    Two instances of the same class are created in this example. When 100 is assigned to MySharedData in the first instance, the same value is then displayed when it is queried from the second instance.

    This is also the case with the MyMember property. When you share property procedures, you must make sure to also share the private variable that stores the value.

  4. To test the code, right-click the project, click Properties, click Test in the Startup Object dialog box, and then click OK to close the project's properties.
  5. Press F5 to run the project.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbHOWTOmaster KB311319 kbAudDeveloper