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
  • 2 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 query is pretty easy to construct, I just
I have a feeling that I already know the answer to this one, but
I somehow have the feeling that modern systems, including runtime libraries, this exception handler
This is an SDL problem, however I have the strong feeling that the problem
I have a feeling that there must be client-server synchronization patterns out there. But
I have a feeling this XML is not valid, can someone please explain why?
I have a feeling that this should really not be all that difficult, yet
I have this code that activates when rollover, rollout, and release. I functions for
I have the feeling that is easy to find samples, tutorials and simple examples
I have the feeling that Flash -based ( or Silverlight -based) websites are generally

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.