How to share the same value across multiple instances of a class

M

Mike

Class A
public objX

I want to create 2 or more instances of Class A and have the same value for
objX in all instances.

Instance1 of Class A
Instance2 of Class A
Instance3 of Class A

Instance1 sets objX = 1
Instance2 and 3 should show objX = 1

Now Instance3 sets objX = 5
Instance2 and 3 should show objX = 5

Thanks
 
M

Michael Letterle

Use another class to old the value of obj x, for example (and this is
terrible code, but you should get the idea):

public class App1
{

[STAThread]
static void Main()
{

B b = new B();
A a = new A();
a._b = b;
A a2 = new A();
a2._b = b;
A a3 = new A();
a3._b = b;

b.i = 1;

Console.WriteLine("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);

b.i = 5;

Console.WriteLine("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);


}


}

public class A
{
public B _b;
public int i
{
get{ return _b.i;}
}


}

public class B
{
public int i;
}
}
 
M

Mike

Thanks Michael
Also, I found this link to be helpful
http://support.microsoft.com/kb/815778

Michael Letterle said:
Use another class to old the value of obj x, for example (and this is
terrible code, but you should get the idea):

public class App1
{

[STAThread]
static void Main()
{

B b = new B();
A a = new A();
a._b = b;
A a2 = new A();
a2._b = b;
A a3 = new A();
a3._b = b;

b.i = 1;

Console.WriteLine("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);

b.i = 5;

Console.WriteLine("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);


}


}

public class A
{
public B _b;
public int i
{
get{ return _b.i;}
}


}

public class B
{
public int i;
}
}

Class A
public objX

I want to create 2 or more instances of Class A and have the same value
for
objX in all instances.

Instance1 of Class A
Instance2 of Class A
Instance3 of Class A

Instance1 sets objX = 1
Instance2 and 3 should show objX = 1

Now Instance3 sets objX = 5
Instance2 and 3 should show objX = 5

Thanks
 
B

Bruce Wood

This approach is appropriate in only two situations:

1. You want something shared across multiple instances of a class, but
not all instances of a class, or, alternatively, you want some
instances to share one piece of information while other instances of
that same class share another piece of information. By way of
illustration, if we take Michael's original definitions for class A and
B:

A a1 = new A();
A a2 = new A();
A a3 = new A();
A a4 = new A();
A a5 = new A();
B b1 = new B();
B b2 = new B();

a1._b = b1;
a2._b = b2;
a3._b = b1;
a4._b = b1;
a5._b = b2;

Now there are two pieces of shared information: b1 and b2. Some
instances of A share b1, while others share b2.

2. The only other situation in which you want to use this idiom is if
the shared information represents some real-world object, "All
employees share a health plan." Making a HealthPlan class and having
all employees point to their health plan does two things: first, it
gives a real-world thing a realization in the class hierarchy, and
second, it allows for the _possibility_ that someday different
employees may have different health plans.

For the most part, though, static fields are the way to go. This
associates a single piece of information with all instances of a class:

public class A
{
private static B _b;

// Typically, you would expose this via a static property rather
than an instance property:
public static B B
{
get { return A._b; }
set { A._b = value; }
}
}

Michael said:
Use another class to old the value of obj x, for example (and this is
terrible code, but you should get the idea):

public class App1
{

[STAThread]
static void Main()
{

B b = new B();
A a = new A();
a._b = b;
A a2 = new A();
a2._b = b;
A a3 = new A();
a3._b = b;

b.i = 1;

Console.WriteLine("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);

b.i = 5;

Console.WriteLine("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);


}


}

public class A
{
public B _b;
public int i
{
get{ return _b.i;}
}


}

public class B
{
public int i;
}
}

Class A
public objX

I want to create 2 or more instances of Class A and have the same value for
objX in all instances.

Instance1 of Class A
Instance2 of Class A
Instance3 of Class A

Instance1 sets objX = 1
Instance2 and 3 should show objX = 1

Now Instance3 sets objX = 5
Instance2 and 3 should show objX = 5

Thanks
 
S

Sericinus hunter

I think the question is really about static modifier.

Michael said:
Use another class to old the value of obj x, for example (and this is
terrible code, but you should get the idea):

public class App1
{

[STAThread]
static void Main()
{

B b = new B();
A a = new A();
a._b = b;
A a2 = new A();
a2._b = b;
A a3 = new A();
a3._b = b;

b.i = 1;

Console.WriteLine("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);

b.i = 5;

Console.WriteLine("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);


}


}

public class A
{
public B _b;
public int i
{
get{ return _b.i;}
}


}

public class B
{
public int i;
}
}

Class A
public objX

I want to create 2 or more instances of Class A and have the same value for
objX in all instances.

Instance1 of Class A
Instance2 of Class A
Instance3 of Class A

Instance1 sets objX = 1
Instance2 and 3 should show objX = 1

Now Instance3 sets objX = 5
Instance2 and 3 should show objX = 5

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top