OO Design - Object Accessibility

This is less a c# question, and more of an object oriented design question in general, but since I'm using c#, I'm posting it in here.

I am a self taught programmer (books, tinkering, etc) and I seem to be having the same problem over and over again when I design more complex apps.  Here's an example:

Say that there are three classes: Class A, B, and C.

Class A contains an instance of Class B, and Class B contains an instance of Class C.

However, both Class B and Class C need to access an object that's a member of Class A.

The only way i can think of to accomplish this, is to add an instance of the object to Class B and C, and pass the required object from Class A by reference.

A. Is there a better way to do this?
B. Is this problem a result of bad design?  And if so, how could the design be changed to avoid it?
Who is Participating?
wolfladConnect With a Mentor Commented:
Let A and B keep its arrays of B and C. In the constructor of B and C make sure you take in a reference to class A.

Here is a working example of a way to do it:

      public class A{
            public string theObject = "HOHOHO";
            B[] arrb = null;

            public A(){
                  arrb = new B[10];
                  for(int x=0;x<10;x++){
                        arrb[x] = new B(this);

      public class B{
            private A myRefToA = null;

            C[] arrb = null;

            public B(A inst){
                  myRefToA = inst;
                  System.Diagnostics.Debug.WriteLine("The A object from B: " + myRefToA.theObject);
                  arrb = new C[10];
                  for(int x=0;x<10;x++){
                        arrb[x] = new C(inst);

      public class C{
            private A myRefToA = null;
            public C(A inst){
                  myRefToA = inst;
                  System.Diagnostics.Debug.WriteLine("The A object from C: " + myRefToA.theObject);

Of course if you made A as a Singleton class (only 1 instance) or static class, you'd have no reason to pass the reference to class B and C.

You are referring to classes and not instances of the classes so my suggestion would be inheritance:

Class A is the topclass. Class B inherits class A and class C inherits class B.

That way class C will have access to all methods & objects of class A & B, while B will have access to all methods & objects of class A.


If A. is correct then it might be caused by bad design.
If class B is just class A with extra objects & method it should inherit A. Same goes for C to B.

If however class B is a huge class that utilizes a lot of other classes and A is just one of them (and a minor part), I would go for the way you described: Class B just uses class A.


If you are talking about instances of the classes however you might want to look into Singleton'ing some of the classes.

If you follow your own design, class C would be able to access class A, through class B if the instance of class A is public in class B.

Just my opinion.

jdraper3Author Commented:
It's laid out so that Class A contains an array of instances of class B, which contain instances of Class C.

Class B and C both need to access objects that are part of Class A.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.