473,769 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3146
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.WriteLi ne("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);

b.i = 5;

Console.WriteLi ne("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.comwro te in message
news:11******** **************@ 73g2000cwn.goog legroups.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.WriteLi ne("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);

b.i = 5;

Console.WriteLi ne("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.WriteLi ne("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);

b.i = 5;

Console.WriteLi ne("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.WriteLi ne("a: {0}\na2: {1}\na3:{2}", a.i, a2.i, a3.i);

b.i = 5;

Console.WriteLi ne("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
5716
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 information should be accessed by multiple projects. How do I archieve this? Any tutorial out there? Thanks
5
2070
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 accessible by a few different dynamic library files (yyy.dll, …). I’ve tried to create a global class object of CData*, say pData, by declaring it as an external in the header and initiate it in the cpp of a dll file. But it doesn’t work. Other dlls...
8
4556
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 explain.) The simple version of the system is like this : A customer inserts HTML code on his webpage, which contacts my statistics server each time the customers website recieves a hit - like a classic "register website traffic" system. ...
11
1576
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 contains function SetProp(). When App1 calls it, a property in the DLL is set to "abc". App2 calls a function GetProp(), in the same DLL. GetProp() should return "abc". This sounds like a simple thing to do, but making the DLL variable shared and...
5
1454
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 b) Storing the object in a static member variable (utilizing a singleton design pattern) Any recommendations on which method would be more suitable? What happens in each of these cases if IIS decides to spawn a new app
15
5816
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 non-website code? -Pravin
6
6917
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 user actions, I want to create n number instances of those classes, which is obviously unknown at compile time. What is the easiest way to create and then manage these kinds of objects? Using an array seems intuitive but it also seems like it...
3
9180
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 arguments that would tell on which interpreter to run the string...? Marcin
5
1761
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 the same instance across classes. #include <iostream> #include <vector>
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6668
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.