I am working on a business issue in C # .NET. I have two classes named C and W that will be created independently at different times.
An object of class C must contain references to 0 ... n objects of class W, that is, an object C can contain up to n objects of W.
Each object W must contain a reference to exactly one object of class C, i.e. object W is contained in one object C.
An object of class C is usually created first. At a later stage, its W-content is discovered and created. At this later stage, I need to cross-reference the objects C and W to each other.
What is a good design template for this? In fact, I have cases where I have three or four classes, but we can talk about two classes to make it simple.
I was thinking of something simple:
class C { public List<W> contentsW; } class W { public C containerC; }
This will work for now, but I can anticipate writing enough code to keep track of all the links and their validity. I would like to implement the code along the way to make minor updates to just the container and deep updates to all the reference classes. Are there any other approaches and what are their advantages?
Change to 11/3: Thank you all for the good answers and good discussion. I finally chose the jop answer because it was closest to what I wanted to do, but other answers also helped. Thanks again!
Kevin P.
source share