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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:33:32+00:00 2026-06-06T11:33:32+00:00

May be I can’t explain exactly in words what I am trying to achieve,

  • 0

May be I can’t explain exactly in words what I am trying to achieve, but I think this sample code can do:

class A
{
    public string Name
    {
        get;
        set;
    }
    public virtual void Say()
    {
        Console.WriteLine("I am A");
        Console.Read();
    }

    public bool ExtendA;

    public A GetObject()
    {
        if (ExtendA)
            return new B();

        return this;
    }
}

internal class B : A
{
    public override void Say()
    {
        Console.WriteLine(string.Format("I am {0}",Name));
        Console.Read();
    }
}

class Program
{
    static void Main(string[] args)
    {          
       var a = new A() {ExtendA = true,Name="MyName"};
        A ab = a.GetObject();            
    } 
}

As per the above code when the field Exitend A is set to true, and when again I try to get the object of same type from the same instance of object, I get the object but it loose the value of property ‘Name’.

Any suggestion how can I return back the class B with properties of A?

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-06-06T11:33:33+00:00Added an answer on June 6, 2026 at 11:33 am

    I suggest you read a book or other resource on design patterns. For this you’d use a factory pattern.

    You’d have your base class A and a class AFactory in addition to class B : A and a class BFactory.

    At runtime you’d choose what you want to instantiate: A or B by using the factories:

    IFactory factory = iWantClassA ? new AFactory() : new BFactory();
    A a = factory.CreateInstance();

    Although I agree with @ArnaudF in that I don’t see what you’re trying to accomplish. Why not just create subclass B directly?

    UPDATE:

    Having re-read your problem it sounds like you really just need a copy-constructor on your class, like so:

    public class A {
        public A() {
            // Default constructor
        }
        public A(A other) {
            // Copy-constructor. Members of other will be copied into this instance.
        }
    }
    
    public class B : A {
        public B() {
    
        }
        public B(A other) : base(other) { // Notice how it calls the parent class's copy-constructor too
        }
    }
    

    Then to “upgrade” an instance of A to an instance of B at runtime, just use the copy constructor:

    A a = new A();
    // some logic here
    B upgradedA = new B( a );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may fall under you can't, and there's no reason to anyway, but I'm
This may be a simple question but I can;t find the answer anywhere. Here
This may be a basic question but how can I include a module with
May be obvious but I tried searching and can't figure it out. I am
May be a stupid questions but can I protect a row of data in
I have a code but can't find where is an error. I have upcoming
Can anyone may explain me why creating object via string is slower than same
I gave up on this one. May be someone can see something obvious. I
May i ask you how can i calculate the period by deduct two time
May I know know I can trigger system to repaint nth row in JList?

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.