473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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 3109
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
Thanks Michael
Also, I found this link to be helpful
http://support.microsoft.com/kb/815778

"Michael Letterle" <mi**************@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
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 #3
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
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 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 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: TaeHo Yoo | last post by:
Hi all, I have a solution which contains multiple projects. Those multiple projects should share the same session. For example, users login, create the session for users then these session...
5
by: Rich | last post by:
Hi there, For a quite big application, I need to get large amount of data within a static library (xxx.lib) and put them in a database (a class, say we call it CData), and then make it...
8
by: Tony Fonager | last post by:
I am currently developing a statistics system in ASP.NET, and need to share information about the customers websites, in this application. (I have simplified my code, to make my project easier to...
11
by: Charles Law | last post by:
I have just been asked how to share functions and properties between two running applications. For example, I have App1 and App2 both running on the same machine. App1 uses a DLL (perhaps) that...
5
by: newjazzharmony | last post by:
I want to share one instance of an object across an ASP dot net application, and I'm trying to weigh the pros and cons of the following two approaches: a) Storing the object in ApplicationState...
15
by: Neo | last post by:
Hello All, I found that ASP.net website only accepts code withing site directory. This creates big hurdle in shairng code. How to share code between two websites, like the way share between two...
6
by: Bugs | last post by:
Does anyone have any recommendations on the best way to create multiple instances of the same class when the final number of instances is unknown? For example, I have a class and based on some...
3
by: Marcin Kalicinski | last post by:
How do I use multiple Python interpreters within the same process? I know there's a function Py_NewInterpreter. However, how do I use functions like Py_RunString etc. with it? They don't take any...
5
by: Jim Langston | last post by:
The output of the following program for me is: Same 0 1 2 Which is what I want. I just want to confirm this is well defined behavior, that a static local variable to a class function/method is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.