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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:54:55+00:00 2026-05-15T23:54:55+00:00

I have two classes, A and B. B knows about A, and A doesn’t

  • 0

I have two classes, A and B. B knows about A, and A doesn’t know about B. B has properties that can be nicely set from A, although there is no inheritance shared between A and B. There will be many times when I need to assign a B’s properties from an A, but I’m looking for pointers on where I should put that code.

public class A
{

}

public class B
{
    //constructor?
    public B(A a)
    {
        //set b's properties from a
    }

    //factory method?
    public static B FromA(A a)
    {
        B b = new B();
        //set b's properties from a
        return b;
    }

    //setter method?
    public static void SetBFromA(B b, A a)
    {
        //set b's properties from a
    }

    //assignment method?
    public void AssignFrom(A a)
    {
        //set b's properties from a
    }
}

//static helper class?
public static class BHelper
{
    public static B GetBFromA(A a)
    {
        B b = new B();
        //set b's properties from a
        return b;
    }

    public static void SetBFromA(B b, A a)
    {
        //set b's properties from a
    }
}

Which, if any, of these are common practice? Do any of them carry implications based on their signature? For example, would using the constructor usually convey that the B is holding a reference to the A passed in? These are the kinds of considerations I’m thinking about.

Thanks!

  • 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-15T23:54:56+00:00Added an answer on May 15, 2026 at 11:54 pm

    All of the options you mention are valid. Which you would choose would depend on the particular circumstances.

     //constructor?
    public B(A a)
    {
        //set b's properties from a
    }
    

    The constructor pattern would be the choice when B cannot exist without the data from A.

    //factory method?
    public static B FromA(A a)
    {
        B b = new B();
        //set b's properties from a
        return b;
    }
    

    The factory method would be the pattern of choice when the values B gets are dependant on values in A or B. Especially if B has descendants that a value in A would provide a choice of construction.

    //setter method?
    public static void SetBFromA(B b, A a)
    {
        //set b's properties from a
    }
    

    The setter method would not be applicable in your scenario as this would be used when A and B do not know of each other and a third party needs to intercede.

    //assignment method?
    public void AssignFrom(A a)
    {
        //set b's properties from a
    }
    

    This is essentially a visitor pattern. A visits B and leaves something of itself (thinks of bachelor days). This would be the pattern of choice if the data from A is optional for B.

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

Sidebar

Related Questions

I have two classes, one that inherits from the other. The base class is
I have two list items that, when clicked, should change classes from '.off' to
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes A and B generated by cxf-codegen-plugin from my WSDL. A
I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, Item and SoldItem . SoldItem inherits from Item adding some
I have two classes set up as follows: class Point { protected: double coords[3];
I have two classes, class A and class B. Class A has a mission
In C++ I have the following two classes that I expose (using Boost) to
I have got a question about binding in Guice. I have got two classes

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.