445,857 Members | 1,816 Online Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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 Dec 8 '06 #1
4 Replies

 P: n/a 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; } } Mike wrote: 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 Dec 8 '06 #2

 P: n/a Thanks Michael Also, I found this link to be helpful http://support.microsoft.com/kb/815778 "Michael Letterle" Class A public objXI want to create 2 or more instances of Class A and have the same valueforobjX in all instances.Instance1 of Class AInstance2 of Class AInstance3 of Class AInstance1 sets objX = 1Instance2 and 3 should show objX = 1Now Instance3 sets objX = 5Instance2 and 3 should show objX = 5Thanks Dec 8 '06 #3

 P: n/a 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 Letterle wrote: 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; } } Mike wrote: 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 Dec 8 '06 #4

 P: n/a I think the question is really about static modifier. Michael Letterle wrote: 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; } } Mike wrote: >Class A public objXI want to create 2 or more instances of Class A and have the same value forobjX in all instances.Instance1 of Class AInstance2 of Class AInstance3 of Class AInstance1 sets objX = 1Instance2 and 3 should show objX = 1Now Instance3 sets objX = 5Instance2 and 3 should show objX = 5Thanks Dec 9 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion. 