473,406 Members | 2,293 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,406 software developers and data experts.

Parent-child classes and circular references

Hi.

I've a problem with parent-child circular references.

Suppose have two classes, Person and Course:
--------------------------
public class Person{
public Person(string name, Course course){
this.name = name;
this.course = course;
}

string name;
public string Name { get{ return name;} }

Course course;
public Course Course { get{ return course;} }
}

public class Course{
public Course(string name, IList<Personpersons){
this.name = name;
this.persons = persons;
}

string name;
public string Name { get{ return name;} }

IList<Personpersons;
public Person Perons { get{ return persons;} }
}
--------------------------

How can I initialize a Person and a Course with their constructors?
Which class to create before?

Suppose that constructors check for not null.

Is this a correct solution (I intend for design, not for compile :-) )?
--------------------------
IList<Personpersons = new List<Person>();
Course math = new Course("Math", persons);
Person bill = new Person("Bill", math);
persons.Add(bill);
--------------------------

How can I solve this problem?

Matteo Migliore.
Sep 27 '07 #1
4 3068

My suggestion is to have the people list instantiated by Course
constructor and have the Person constructor assign itself to the
passed course. That reduces your calling code to two lines--one to
create course, one to create person.

HTH,

Sam
class Course {
private IList<Personpeople = new List<Person>();
private string name;

public Course(string name) {
this.name = name;
}
public IList<PersonPeople { get { return people; } }
}

class Person {
private string name;
public Person(string name, Course course) {
this.name = name;
course.People.Add(this);
}
}
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 27 Sep 2007 06:31:52 +0200, "Matteo Migliore"
<ma*****************@gmail.comwrote:
>Hi.

I've a problem with parent-child circular references.

Suppose have two classes, Person and Course:
--------------------------
public class Person{
public Person(string name, Course course){
this.name = name;
this.course = course;
}

string name;
public string Name { get{ return name;} }

Course course;
public Course Course { get{ return course;} }
}

public class Course{
public Course(string name, IList<Personpersons){
this.name = name;
this.persons = persons;
}

string name;
public string Name { get{ return name;} }

IList<Personpersons;
public Person Perons { get{ return persons;} }
}
--------------------------

How can I initialize a Person and a Course with their constructors?
Which class to create before?

Suppose that constructors check for not null.

Is this a correct solution (I intend for design, not for compile :-) )?
--------------------------
IList<Personpersons = new List<Person>();
Course math = new Course("Math", persons);
Person bill = new Person("Bill", math);
persons.Add(bill);
--------------------------

How can I solve this problem?

Matteo Migliore.
Sep 27 '07 #2
Course[] courses = Registration.GetPersonCourses(person);
Person[] persons = Registration.GetCoursePersons(course);

Registration.AddPersonToCourse(Person, course);

this way we decoupled course and person.
"Matteo Migliore" <ma*****************@gmail.comwrote in message
news:#S**************@TK2MSFTNGP05.phx.gbl...
Hi.

I've a problem with parent-child circular references.

Suppose have two classes, Person and Course:
--------------------------
public class Person{
public Person(string name, Course course){
this.name = name;
this.course = course;
}
string name;
public string Name { get{ return name;} }

Course course;
public Course Course { get{ return course;} }
}

public class Course{
public Course(string name, IList<Personpersons){
this.name = name;
this.persons = persons;
}
string name;
public string Name { get{ return name;} }

IList<Personpersons;
public Person Perons { get{ return persons;} }
}
--------------------------

How can I initialize a Person and a Course with their constructors?
Which class to create before?

Suppose that constructors check for not null.

Is this a correct solution (I intend for design, not for compile :-) )?
--------------------------
IList<Personpersons = new List<Person>();
Course math = new Course("Math", persons);
Person bill = new Person("Bill", math);
persons.Add(bill);
--------------------------

How can I solve this problem?

Matteo Migliore.
Sep 27 '07 #3
My suggestion is to have the people list instantiated by Course
constructor and have the Person constructor assign itself to the
passed course. That reduces your calling code to two lines--one to
create course, one to create person.
Thanks for your answer but the problem is that
IList<PersonPersons of class Course could be a ReadOnlyCollection<T>
in a real-world application.

And I want to check list lenght
when passed to the constructor of class Course.

So, how can I do?

Thx! ;-)
Matteo Migliore.
Sep 27 '07 #4
Matteo Migliore wrote:
>My suggestion is to have the people list instantiated by Course
constructor and have the Person constructor assign itself to the
passed course. That reduces your calling code to two lines--one to
create course, one to create person.

Thanks for your answer but the problem is that
IList<PersonPersons of class Course could be a ReadOnlyCollection<T>
in a real-world application.

And I want to check list lenght
when passed to the constructor of class Course.
If you have imposed the requirement that the collection must be
completely initialized before the Course class is instantiated, then you
have no choice in the matter. You must provide a way to assign a Course
to a Person after the Person has been instantiated.

You may find it makes sense, using that design, to have the Course class
do that adding in its own constructor. So it would enumerate the list,
adding itself to each Person in the list. That at least would clean up
some of the initialization code.

That said, if you can at all do what Samuel suggests, I think you
should. Or at least something like it. It's not exactly the same, but
the Control.Controls collection is sort of similar; that is, it provides
a method for manipulating a collection that is actually encapsulated in
the class, in a way that understands the relationship between the parent
and child. The constraints you've put on the problem make for some
fairly unwieldy code IMHO.

Also, I find it a little odd that a Person can have only one Course.
Granted, I don't know the context, but in most educational settings, a
single individual can be enrolled in multiple courses, just as a single
course will generally have multiple individuals enrolled.

The other thing that I find odd is the implication in your example code
that you may modify the Course's collection from outside the Course.
This is contrary to your statement that the collection might be
read-only anyway, but even if it weren't it seems like a poor design.
The Course class really ought to be in sole control of the collection of
Persons enrolled in the Course.

Just my two cents.

Pete
Sep 27 '07 #5

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

Similar topics

2
by: Steve | last post by:
Hi, I have a nested class declaration such as: class A: def __init__(self): self.var = "A's variable" # end def class B:
11
by: HolaGoogle | last post by:
Hi, Sorrryy to ask such basic question but i do need your help! Here's what i'm trying to do: In my parent form i'm calling a my Iframe form to get certain value, then depending on that value...
1
by: Paul Gobée | last post by:
What is the parent element of a button (IE6 on Win, transitional mode)? Is it the BODY, the browser default stylesheet, or something else? Contra "body is parent-element": - Buttons with no...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
5
by: Zürcher See | last post by:
The Control class has the Parent property that is readonly. When the control is added to a controls collection of another control the Parent property refers to that control. "Who" set the Parent...
2
by: Jeronimo Bertran | last post by:
Hi, I have a page with a very data intensive grid which needs to be automatically refreshed constantly if a change is detected. In order to not refresh the complete page so often, I created an...
2
by: Roy | last post by:
Ok, this is incredibly annoying. Below I have an ImageButton and it's ImageButton_Click sub. See the 6 response.writes? The output for them is displayed below in asterisks. Question: What part of...
5
by: gnewsgroup | last post by:
In my user control, I would like to find a Label control in the parent page (the page that uses my user control). I need to update that Label.Text when something happens in the user control. I...
1
by: bnchs | last post by:
This is C code. I am trying to fill each node's Parent field with its parent because I am drawing nodes to the screen. However, I have not been able to get this working. Reading the output from the...
4
by: Andrew | last post by:
I want to create a set of Activity Diagram controls for process control. I need to create a base Diagram control that acts as a container for the Activity controls ( StartPoint, EndPoint,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...
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...
0
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,...
0
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...

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.