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

  • SEARCH
  • Home
  • 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 7985239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:30:03+00:00 2026-06-04T11:30:03+00:00

I am having a problem with creating 2 immutable objects where both of them

  • 0

I am having a problem with creating 2 immutable objects where both of them have a dependency on each other. Question: How do I solve this situation keeping these objects immutable?

public class One
{
    private readonly Another another;
    public One(Another another)
    {
        this.another = another;
    }
}

public class Another
{
    private readonly One one;
    public Another(One one)
    {
        this.one = one;
    }
}
  • 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. Editorial Team
    Editorial Team
    2026-06-04T11:30:04+00:00Added an answer on June 4, 2026 at 11:30 am

    Its not possible to do what you suggest, unless you at least allow for dependency injection on one of the classes, as follows:

    public class One
    {
        private readonly Another another;
        public One(Another another)
        {
            this.another = another;
        }
    }
    
    public class Another
    {
        private readonly One one;
        public Another(One one)
        {
            this.one = one;
        }
        public Another() {}
        public setOne(One one)
        {
           this.one = one;
        }
    }
    

    You may then have to consider putting some sort of protection logic (Exceptions?) in Another.setOne() so that the One object can only be set once.

    Also consider that you may have problems instantiating Another using the default constructor without initializing the one variable, in which case you may have to remove the readonly attribute and use the aforementioned logic in setOne()

    OR

    You could create the One class and internally have it create the Another class with a reference to One. This might increase the coupling between the two, but would do what you need, as folows:

    public class One
    {
        private readonly Another another;
        public One()
        {
            this.another = new Another(this);
        }
        public Another getAnother()
        {
            return this.another;
        }
    }
    
    public class Another
    {
        private readonly One one;
        public Another(One one)
        {
            this.one = one;
        }
        public Another() {}
    }
    
    ...
    
    One one = new One();
    Another another = one.getAnother();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a problem with creating a navigation controller after on the other
I'm having a problem creating a new CompositeControl. Currently I have a custom control
Im having this weird problem with creating Json dynamically.... for some reason this doesnt
I am new to OOP having this code and am having a problem creating
I'm having a problem with a generic class I'm creating in Scala. I have
I'm trying to create this JSON string but having problem creating it. I'm getting
i am having a problem creating RMIserver class because i keep getting this error
I'm having a problem with creating my styles for a custom wpf class. This
I've been having a problem creating a checklist in the style of the TouchCells
i am having problem in creating a video player in flash via as3, the

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.