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 6103231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:42:20+00:00 2026-05-23T13:42:20+00:00

I have a feeling that this is a code smell, and I could be

  • 0

I have a feeling that this is a code smell, and I could be doing this better, and if that is the case please point it out.

class main
{
  void main()
  {
    object A
    object B

    A = new SystemA(ref B)
    B = new SystemB(ref A)
   }
}
class SystemA
{
  SystemB B;
  public SystemA (ref B)
  {
    this.B = B;
  }
}
class SystemB
{
  SystemA A;
  public SystemA (ref A)
  {
    this.A = A;
  }
}

Basically I need to initialize two classes with references to each other.

This doesn’t work and generates a null reference exception for the class fields in the two child classes when they are later accessed after initialization.

I realize I could probably set these after initialization, but this adds a touch more bloat that I would like to avoid, since I view these assignments as initialization actions.

  • 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-05-23T13:42:20+00:00Added an answer on May 23, 2026 at 1:42 pm

    If you gave code which actually compiled, I suspect it would just end up with SystemA storing a reference to null – because that’s the value of B when you construct A.

    Having two classes which need a reference to each other is definitely a code smell, but you simply can’t make them both refer to each other, unless one of them constructs the other and passes in “this”, e.g.

    class SystemA
    {
        private readonly SystemB systemB;
    
        public SystemA()
        {
            systemB = new SystemB(this);
        }
    }
    
    class SystemB
    {
        private readonly SystemA systemA;
    
        public SystemB(SystemA systemA)
        {
            this.systemA = systemA;
        }
    }
    

    Now if you need access to both values afterwards, you could either put a property in one of them giving access to the other, or (and this is really nasty) use an out parameter in the constructor:

    public SystemA(out systemB)
    {
        systemB = new SystemB(this);
        this.systemB = systemB;
    }
    

    Then call it as:

    SystemB b;
    SystemA a = new SystemA(out b);
    

    Please don’t do this though.

    • It’s horrendously complicated
    • It’s generally a bad idea to let this escape a constructor anyway
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling that this should really not be all that difficult, yet
I am trying to understand the code in this page: https://github.com/corroded/git-achievements/blob/gh-pages/git-achievements and I'm kinda
I have this function: $(function() { $('.upvote').click(function() { voteActions(upvoteID, this.id); } ); $('.downvote').click(function() {
So I'm finally learning my way around CSS more and have run into a
Here I am writing a small app with the sole intent of acquiring better
I currently have a RoR application set up in a (semi-)production environment on a
I am using the following code to get today's date. Calendar cal = Calendar.getInstance();
In VirtualTreeview, I am storing my data in the PVirtualNodes . I have experienced
Ugh - not sure why I'm having so much trouble with this. Writing a
There are actually three views at play. My homeView, which is where I launch

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.