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