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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:16:54+00:00 2026-05-27T16:16:54+00:00

I have a base class public class A { public string s1; public string

  • 0

I have a base class

public class A   
{
    public string s1;
    public string s2;
}

I also have a derived class :

public class B : A
{
    public string s3;
}

Suppose my program created an instance of class A.

A aClassInstance = new A();

some parameters were set:

aClassInstance.s1 = "string 1";
aClassInstance.s2 = "string 2";

At this point I would like to create an instance of class B. But I would like B to already have the values of my instance of class A.

This DID NOT Work:

public B bClassInstance = new B():
bClassInstance = (B)aClassInstance;

NEITHER DID THIS:

Made a clone method within Class A.

public B cloneA() {    
    A a = new A();
    a = (A)this.MemberwiseClone()
    return(B)a;
}

The VS code takes both of the above – but I get run-time errors

Please help

  • 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-27T16:16:55+00:00Added an answer on May 27, 2026 at 4:16 pm

    The base problem you have is, that you have to construct an instance of type B (which contains of cause the properties of type A). Your approach to clone an A instance won’t work, because that gives you an instance of type A, which you can’t convert to B.

    I would write constructors for class A and B which takes a parameter of type A. The constructor of class B just passes the value to its base class A. The constructor of class A knows how to copy the fields to itself:

    class A {
        public A(A copyMe) {
            s1 = copyMe.s1;
            ...
        }
    
    class B : A {
    
        public B(A aInstance) : base(aInstance) {
        }
    
    }
    

    Use it this way:

    A a = new A();
    a.s1 = "...";
    
    B b = new B(a);
    

    EDIT

    When you don’t want to have to change the constructor of A when adding new fields or props, you could use reflection to copy the properties. Either use a custom attribute to decorate what you want to copy, or copy just all props/fields of A:

    public A (A copyMe) {
        Type t = copyMe.GetType();
        foreach (FieldInfo fieldInf in t.GetFields())
        {
            fieldInf.SetValue(this, fieldInf.GetValue(copyMe));
        }
        foreach (PropertyInfo propInf in t.GetProperties())
        {
            propInf.SetValue(this, propInf.GetValue(copyMe));
        }
    }
    

    I havn’t tried the code, but the point should become clear.

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

Sidebar

Related Questions

I have an abstract base class and derived class: type TInterfaceMethod = class public
Assume some domain and view objects (that have no common base class) public class
Suppose I have the following (trivially simple) base class: public class Simple { public
I have a base class, defined as below (I'm also using DevExpress components): public
Suppose that we have the following base and derived classes: #include <string> #include <iostream>
If I have a base class such that public abstract class XMLSubscription <T extends
I have an abstract base class with a TcpClient field: public abstract class ControllerBase
Let's say we have these two classes: public class Base { public static int
I have an abstract base class class AbstractClass { Col<AbstractClass> parent public AbstractClass() {
I have these classes in C# (.NET Framework 3.5) described below: public class Base

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.