473,624 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5676
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.specialVal ue; }
set { this.specialVal ue = value; }
}
}

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

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

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
314
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 executes the same function they can interfere with one another as these functions are the same for all instances of the class (and thus internal variables
4
11727
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 session variable. I have two different applications right now that effectively perform the same action at one point. In the one application I created a shared variable:
1
1052
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 variables, running this in a ASP.NET application gave trouble of course, due to the fact it is shared not within the session but within the application.
10
3672
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' variable for the application. Any other code within the application can access it. public - can be shared across the application if instatiated. Does that sound about right? It seems these are more useful for methods rather than variables. Most of...
9
5392
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 for each thread, I put in Shared methods as below. It is only now that I am realizing the complexity of multiple threads accessing shared methods. And, quite honestly, I am very confused. I have tried System.Threading.Monitor.Enter, Synclock,...
8
1265
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 private to the class, each object would have access to it's ver own copy of that variable. With regard to the methods defined for a class, I assumed that these are shared among all objects of the class, the code acting on the instances own copy...
5
4385
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 things in a multi user environment. In other words, are my shared classes for the want of a better word. "Instantiated" for each user. Hope this makes sense ! --
2
7022
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 used inside the shared function are shared as well, is it same for session variables...? example vb.net;
15
2760
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 Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.' So, does this mean All the static/shared methods written in .NET compatible programming language, such as C#, VB.NET, are guaranteed to be...
0
972
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 first access of dll and use them then on thus avoiding any more database hits. when i use the dll on my machine, it works perfect. i.e., it reads the values from database, saves them to shared variables on one time basis. but when i installed the...
0
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
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
8620
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
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...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
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
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1784
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.