Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 63247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:36:10+00:00 2026-05-10T18:36:10+00:00

I am working on a business problem in C#.NET. I have two classes, named

  • 0

I am working on a business problem in C#.NET. I have two classes, named C and W that will be instantiated independently at different times.

An object of class C needs to contain references to 0 … n objects of class W, i.e. a C object can contain up to n W objects.

Each W object needs to contain a reference to exactly 1 object of class C, i.e. a W object is contained in one C object.

An object of class C is usually instantiated first. At a later point, its W contents are discovered, and instantiated. At this later point, I need to cross reference the C and W objects to each other.

What is a good design pattern for this? I actually have cases where I have three or four classes involved but we can talk about two classes to keep it simple.

I was thinking of something simple like:

class C {    public List<W> contentsW;  }  class W {   public C containerC;  } 

This will work for the moment but I can foresee having to write a fair amount of code to keep track of all the references and their validity. I’d like to implement code down the road to do shallow refreshes of just the container and deep refreshes of all referenced classes. Are there any other approaches and what are their advantages?

Edit on 11/3: Thanks to all for the good answers and good discussion. I finally chose jop’s answer because that came closest to what I wanted to do, but the other answers also helped. Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:36:11+00:00Added an answer on May 10, 2026 at 6:36 pm

    If you have the Martin Fowler’s Refactoring book, just follow the ‘Change Unidirectional Association to Bidirectional’ refactoring.

    In case you don’t have it, here’s how your classes will look like after the refactoring:

    class C {   // Don't to expose this publicly so that    // no one can get behind your back and change    // anything   private List<W> contentsW;     public void Add(W theW)   {     theW.Container = this;   }    public void Remove(W theW)   {     theW.Container = null;   }    #region Only to be used by W   internal void RemoveW(W theW)   {     // do nothing if C does not contain W     if (!contentsW.Contains(theW))        return; // or throw an exception if you consider this illegal     contentsW.Remove(theW);   }    internal void AddW(W theW)   {     if (!contentW.Contains(theW))       contentsW.Add(theW);   }   #endregion }  class W {   private C containerC;    public Container Container   {     get { return containerC; }     set      {        if (containerC != null)         containerC.RemoveW(this);       containerC = value;        if (containerC != null)         containerC.AddW(this);     }   } } 

    Take note that I’ve made the List<W> private. Expose the list of Ws via an enumerator instead of exposing the list directly.

    e.g. public List GetWs() { return this.ContentW.ToList(); }

    The code above handles transfer of ownership properly. Say you have two instances of C — C1 and C2 – and the instances of W — W1 and W2.

    W1.Container = C1; W2.Container = C2; 

    In the code above, C1 contains W1 and C2 contains W2. If you reassign W2 to C1

    W2.Container = C1; 

    Then C2 will have zero items and C1 will have two items – W1 and W2. You can have a floating W

    W2.Container = null; 

    In this case, W2 will be removed from C1’s list and it will have no container. You can also use the Add and Remove methods from C to manipulate W’s containers – so C1.Add(W2) will automatically remove W2 from it’s original container and add it to the new one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a WCF service library where hopefully all the business
Background I am working on a legacy small-business automation system (inventory, sales, procurement, etc.)
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project at the moment and we have to implement soft deletion
Working on a project that parses a log of events, and then updates a
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
Working on a somewhat complex page for configuring customers at work. The setup is
Working with python interactively, it's sometimes necessary to display a result which is some
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.