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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:02:44+00:00 2026-05-13T06:02:44+00:00

The point of this exercise is to make navigation between objects stateful. For example,

  • 0

The point of this exercise is to make navigation between objects stateful.

For example, having Person and Address with 1-1 association it should:

  • If an address is assigned to a persons, then the person should be assigned to the address (and vice versa).
  • If address is assigned to person1 and then to person2, then the person1 will have no address and person2 will.

This is the piece of code that implements it.

public class A {
    internal B a;
    public B Value {
        get {
            return a;
        }
        set {
            if (value == null) {
                if (a != null)
                    a.a = null;
            }  else
                value.a = this;
            a = value;
        }
    }
}

public class B {
    internal A a;
    public A Value {
        get {
            return a;
        }
        set {
            if (value == null) {
                if (a != null)
                    a.a = null;
            }  else
                value.a = this;
            a = value;
        }
    }
}

This allows following tests to pass:

// For the common setup:
var a = new A();
var b = new B();

// Test 1:
a.Value = b;
Assert.AreSame(a, b.Value);

// Test 2:
b.Value = a;
Assert.AreEqual(b, a.Value);

// Test 3:
b.Value = a;
b.Value = null;
Assert.IsNull(a.Value);

// Test 4:
var a2 = new A();
b.Value = a2;
Assert.AreSame(b, a2.Value);
Assert.AreNotSame(a, b.Value);

// Test 5:
a.Value = b;
Assert.AreSame(a, b.Value);
var a1 = new A();
var b1 = new B();
a1.Value = b1;
Assert.AreSame(a1, b1.Value);

// Test 6:
var a1 = new A();
var b1 = new B();
Assert.IsNull(a.Value);
Assert.IsNull(b.Value);
Assert.IsNull(a1.Value);
Assert.IsNull(b1.Value);

Now the question is: how would you abstract the code in the setters to avoid possible mistakes when writing a lot of such classes?

The conditions are:

  • The PUBLIC interfaces of classes A and B cannot be changed.
  • Factories should not be used.
  • Statics should not be used (to persist shared info).
  • ThreadInfo or similar should not be used.
  • 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-13T06:02:44+00:00Added an answer on May 13, 2026 at 6:02 am

    I really don’t understand your challenge. What happens when you instantiate a few instances of class A and then a single instance of class B?

    A a1 = new A();
    A a2 = new A();
    A a3 = new A();
    A a4 = new A();
    A a5 = new A();
    B b = new B();
    

    Which test passes? Which one fails?

    You see, once A is instantiated, it has a state. This state should be somehow involved with an instance of B, an existing instance. So this instance of B must exist even before you instantiate this A class.

    The same is true for an instance of B. It should hold a reference to an already existing instance of A.

    As far as I understand, class A should have a constructor with a reference to an existing instance of B:

    public class A
    {
        private B b;
        public A(B b)
        {
              this.b = b;
        }
    }
    
    // Then you can have:
    B b1 = new B();
    A a1 = new A(b1); // here's the link
    
    B b2 = new B();
    A a2 = new A(b2); // and another link
    

    Either this or the other way around with B.

    You write that you don’t want to change the public signatures of A and B, and you don’t want to add factories to the code. I really can’t see a consistent solution under such constraints. Or maybe the challenge itself is not clear enough?

    EDIT: Taking a wild guess here, I think that what you try to achieve here can be done using Reflection: you might want to reflect existing code up to a point (in the call stack), and match a new instance of, say, A to an existing instance of B. This is can be done using reflection, but it’s pretty hard and you must have a concrete and robust set of rules for the linkage between new instances of As and Bs. If this is the direction of the needed solution, then I think you should dive into reflection and see how it goes, it’s a huge field.

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

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From what I can tell, LHND is not even a… May 14, 2026 at 7:12 pm
  • Editorial Team
    Editorial Team added an answer The EJB specification don't specify how clustering should be achieved,… May 14, 2026 at 7:12 pm
  • Editorial Team
    Editorial Team added an answer An ASP.NET application (by default) will execute in IIS6 under… May 14, 2026 at 7:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.