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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:42:55+00:00 2026-05-18T01:42:55+00:00

Suppose you have two classes, as in the example below. How would you modify

  • 0

Suppose you have two classes, as in the example below.

How would you modify SplitObject such that it always returns an object of type t, such as in Main(), where it should return an object of type DerivedClass?

I’m guessing the solution would involve reflection? I haven’t learned anything about reflection yet, so I don’t know how this would work.

public class BaseClass
{
    float _foo;

    public BaseClass(float foo){_foo = foo}

    public BaseClass SplitObject()
    {
        Type t = GetType();

        // Do something with t 

        _foo = _foo/2f;
        return new BaseClass(_foo); // I want to construct an 
                                   // object of type t instead 
                                  // of type BaseClass
    }
}

public class DerivedClass : BaseClass
{
    public DerivedClass(float foo) : base(foo){}
}

class Program
{
    static void Main() 
    {
        BaseClass foo = new DerivedClass(1f);

        BaseClass bar = foo.SplitObject(); // should return a DerivedObject
    }
}
  • 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-18T01:42:55+00:00Added an answer on May 18, 2026 at 1:42 am

    If you really wanted to use reflection, you could do something like:

    return (BaseClass)Activator.CreateInstance(GetType(), _foo);
    

    Of course, there is now an implicit contract that all derived classes must implement such a constructor. Unfortunately, such contracts cannot be specified in the current type-system; so violations will not be caught at compile-time. It would be much better to go with erash’s idea. I would do something like:

    //... Base class:   
    
    public BaseClass SplitObject()
    {      
        _foo = _foo / 2f;
        return NewInstance(_foo);
    }
    
    protected virtual BaseClass NewInstance(float foo)
    {
       return new BaseClass(foo);   
    }
    
    //... Derived class:
    
    protected override BaseClass NewInstance(float foo)
    {
       return new DerivedClass(foo);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose you have two seperate ASP.NET Web Application projects that both need to use
Suppose that you have two huge files (several GB) that you want to concatenate
I have a class that contains a member object. I would like to call
suppose you have two (or more) classes with private member vectors: class A {
Suppose I have two threads A and B that are both incrementing a ~global~
Suppose I have two applications written in C#. The first is a third party
Suppose you have two models, User and City, joined by a third model CityPermission:
Suppose I have two branches of a project IMClient-MacOS and IMClient-Windows, and their code

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.