473,396 Members | 1,843 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,396 software developers and data experts.

how can I create shared variables in c#

This may sound stupid but I am having trouble figuring out this one. Any
help would be appreciated.

I have 3 classes

ClassA & ClassB and ClassC

From ClassC I would instantiate ClassA
ClassA inturn uses/ instantiates ClassB
I would like to create a variable in ClassA that can be accessed from both
ClassB and ClassC.
I want to set/initialize the value from within ClassA, and then access this
variable/ get its value from ClassB as well as ClassC.
These classes would be instantiated on the web server so the values of the
variables for different requests would be different so as such I can't use a
static variable.

Thanks in advance

NU

Nov 17 '05 #1
1 5661
The code below should answer your question directly. However, it is
probably bad form to have all of these classes know about each other.
It would be better to create an interface for A. Only B would know
about the actual concrete type of A (when it creates the instance). All
public references to A would be through its interface.
class A {
// classA uses/instantiates ClassB
private B classB;
public A(){
classB = new B(this);
}

// expose the instance of B as a public property
public B ClassB {
get { return this.classB ; }
}

private object specialValue;
public object SpecialValue {
get { return this.specialValue; }
set { this.specialValue = value; }
}
}

class B {
A myCreatorA
public B(A myCreatorA){
this.myCreatorA = myCreatorA;
}

public void DoSomethingInB(){
// access data in A from B
object x = myCreatorA.SpecialValue;
}
}

class C {
A myCreatedA;

// classC instantiates ClassA
public C(){
this.myCreatedA = new A();
}

public A MyCreatedA{
get { return this.myCreatedA; }
}

private void DoSomethingInC(){
// access data in A from C
object x = this.myCreatedA.SpecialValue;
}
}

News User wrote:
This may sound stupid but I am having trouble figuring out this one. Any
help would be appreciated.

I have 3 classes

ClassA & ClassB and ClassC

From ClassC I would instantiate ClassA
ClassA inturn uses/ instantiates ClassB
I would like to create a variable in ClassA that can be accessed from both
ClassB and ClassC.
I want to set/initialize the value from within ClassA, and then access this
variable/ get its value from ClassB as well as ClassC.
These classes would be instantiated on the web server so the values of the
variables for different requests would be different so as such I can't use a
static variable.

Thanks in advance

NU

Nov 17 '05 #2

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

Similar topics

3
by: tom | last post by:
Hello, I'm using the ms data access application blocks. My question is about the fact that everywhere shared methods are used. Does this mean that when more than 1 user at the same time...
4
by: John Kraft | last post by:
Hi all, My question is more of a phylisophical one here, but I am wondering what the difference is (effectively and performance wise) between using a shared variable/static variable and using a...
1
by: Fredrik Melin | last post by:
Hi, I have a support library with all functionallity I need to access my business application on the web. The problem is that Connection object, User object and some other objects are Shared...
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
8
by: gemel | last post by:
I have been reading sime material in .NET that throws some doubt on my understanding of shared procedures. With regard to object programming I assumed that variables declared within a class were...
5
by: Confused ! | last post by:
I am writing an ASP.NET application and I want to use shared members on the data layer to retreive information from the SQL Server. If I declare a Function to be Shared, how will this affect...
2
by: Ibrahim. | last post by:
Hello, My question is simple. If Session variables were to be assigned a value inside a shared function, does the session data visible to other sessions (Other users). I know the variables...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
0
by: netkadirisani | last post by:
Hi, i built a dll (used vb.net) that has some shared variables. the basic idea of using the shared variables is to read some values from the database and save them in the shared variables during the...
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: 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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.