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
topRequirements
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
topAdditional 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 topStep-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 "I am shared function.";
}
}
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("SharedData accessed by the first instance of ClsDemo {0}",objDemo1.MyInstanceMember());
Console.WriteLine("SharedData accessed by the second instance of ClsDemo {0}",objDemo2.MyInstanceMember());
Console.WriteLine("UnSharedData accessed by the first instance of ClsDemo {0}",objDemo1.MyMember);
Console.WriteLine("UnSharedData accessed by the second instance of ClsDemo {0}",objDemo2.MyMember);
// Access the shared property.
ClsDemo.MySharedProperty = "I am shared property.";
Console.WriteLine(ClsDemo.MySharedProperty);
// Access the shared function.
Console.WriteLine(ClsDemo.MySharedFunction());
Console.WriteLine("Press the ENTER key...");
Console.ReadLine();
}
}
- Save the project.
- To run the project, click Start on the Debug menu.
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