Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

OO Design - Object Accessibility

Posted on 2004-07-30
5
194 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:jdraper3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:wolflad
ID: 11677112
A.

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.

B.

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.

-Wolf5
0
 
LVL 1

Author Comment

by:jdraper3
ID: 11680910
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.

0
 
LVL 1

Accepted Solution

by:
wolflad earned 500 total points
ID: 11681125
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading the Web.Config using IIS 7.5? 4 47
What namespace do I need to import? 2 38
C# bracket error 3 35
insert value of checklistbox checked 4 32
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question