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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:39:41+00:00 2026-06-10T21:39:41+00:00

I have two classes, one which inherits the other, for example:- public class A

  • 0

I have two classes, one which inherits the other, for example:-

public class A
{
    public string AA { get; set; }
    public double AB { get; set; }
}

and

public class B : A
{
    public double? BA { get; set; }
}

Now, I know that if I have an instance of object B, I can cast that as type A, but I have an instance of A that I want to instantiate as a type B object. I know I could put a constructor in B’s class which copies all the property values over, but the base class, A, has quite a few properties and may well have more added, so I’m worried about missing one or forgetting to add it in to B’s constructor when I add it to A. Is there an easy way of doing this, please?

——-Edit——

Thanks folks, not my use-case unfortunately, it’s a question a mate’s fired at me. As far as I’m aware there are issues and he can’t change the base class. As I understand it, the base class is one that hooks into a database call, he then wants to grab that data and add some extra properties for an admin editing screen which his code will then use to update the base data object’s properties. My first reaction was to have the base object as a property of his object, but there’s a reason it’s significantly easier for him to have it “flattened” with the tools he’s using. I didn’t think there was a way of doing this, but I thought I’d check in case I was missing something – I thought it would be easier to just ask the basic question, rather than include the background, which would probably just raise more questions and muddy the water (AFAIK he’s suffering from “circumstances beyond his control”, with this).

  • 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-10T21:39:43+00:00Added an answer on June 10, 2026 at 9:39 pm

    I know I could put a constructor in B’s class which copies all the property values over, but the base class, A, has quite a few properties and may well have more added, so I’m worried about missing one or forgetting to add it in to B’s constructor when I add it to A. Is there an easy way of doing this, please?

    Typically, this is a sign of a design flaw. I would rethink your design first, and determine whether this is truly necessary, or if there is another approach you can use here. That being said…

    There is no easy way to do this. If you need a B instance to be created from your A instance, you’ll really need to copy the values over to the new instance.

    A better option, however, would potentially be to add a (potentially protected) constructor to A that sets all of these properties, given another A instance. B could then use this to create itself, which at least keeps the requirements for matching all of the properties within the class that defines them. This should make it easier to maintain this over time.

    • 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 of which inherits from the other. Im trying to
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes, one that inherits from the other. The base class is
Actually I have two java classes of which one is an activity class. Now
I have a class that instantiates two classes which implement interfaces. I want one
I have two (unrelated) classes. The first one is Point: typedef std::complex<double> complex_number; class
Hi I have two classes, one called Instruction, one called LDI which inherits from
I need to have two classes, one class has two methods each of which
Say you have these two classes. public class Author { public int ID {get;
i have two data classes which hold only data members(no functions). One is CallTask

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.