Microsoft KB Archive/815778

= How to share data across multiple instances of a class in Visual C# .NET or in Visual C# 2005 =

Article ID: 815778

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





For a Microsoft Visual Basic .NET version of this article, see 311319.

IN THIS TASK

 * SUMMARY
 * Requirements
 * Additional Information
 * Step-By-Step Example



SUMMARY
This article describes how to share data across multiple instances of a class in Visual C# .NET by using static variables.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

This article assumes that you are familiar with the following topics:
 * Visual Studio .NET or Visual Studio 2005
 * Visual C# .NET or Visual C# 2005

back to the top

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 static members of a class provide a way for all instances of the class to use the same memory location or function.

If you prefix one of your data members with the static keyword, Visual C# .NET or Visual C# 2005 creates a single location for that static 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 the other instances.

back to the top

Step-By-Step Example
 Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C# Projects under Project Types, and then click Console Application under Templates. By default, Class1.cs is created.

Note In Visual Studio 2005, click Visual C# under Project Types, and then click CLR Console Application under Templates  Delete all the code that is generated in the Class1.cs file, and then paste the following code: using System;

public class ClsDemo {  public static int MySharedData; public int MyMember; private static string m_myPropertyData;

public int MyInstanceMember {     return MySharedData; }

public static string MySharedProperty {     get {        return m_myPropertyData; }     set {        m_myPropertyData = value; }  }

public static string MySharedFunction {     return &quot;I am shared function.&quot;; } }

class class1 {  static  void Main {        ClsDemo.MySharedData = 100; // Create an object of the ClsDemo class. ClsDemo objDemo1 = new ClsDemo; // Create a second object of the ClsDemo class. ClsDemo objDemo2 = new ClsDemo;

//Initialize MyMember for the objDemo1 object. objDemo1.MyMember = 120; Console.WriteLine(&quot;SharedData accessed by the first instance of ClsDemo {0}&quot;,objDemo1.MyInstanceMember); Console.WriteLine(&quot;SharedData accessed by the second instance of ClsDemo {0}&quot;,objDemo2.MyInstanceMember); Console.WriteLine(&quot;UnSharedData accessed by the first instance of ClsDemo {0}&quot;,objDemo1.MyMember); Console.WriteLine(&quot;UnSharedData accessed by the second instance of ClsDemo {0}&quot;,objDemo2.MyMember);

// Access the shared property. ClsDemo.MySharedProperty = &quot;I am shared property.&quot;; Console.WriteLine(ClsDemo.MySharedProperty); // Access the shared function. Console.WriteLine(ClsDemo.MySharedFunction);

Console.WriteLine(&quot;Press the ENTER key...&quot;); Console.ReadLine; } }  Save the project. To run the project, click Start on the Debug menu.</ol>

Two instances of the same class are created in this example. When the 100 value is assigned to the MySharedData variable in the first instance, the same value (100) is displayed when the value is queried for from the second instance. This is not the case with the MyMember variable. Because the MyMember variable is not prefixed with the static keyword, the variable cannot be shared between the multiple instances that are created in the sample piece of code.

back to the top

Keywords: kbnamespace kbprogramming kbhowtomaster KB815778

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.