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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:28:02+00:00 2026-06-05T21:28:02+00:00

I have to write 3 functions which accepts a parameter of BaseClass type and

  • 0

I have to write 3 functions which accepts a parameter of BaseClass type and then return a DerivedClasses as the return value.

Public DerivedClass1 SimpleTest1(BaseClass baseType)
{
DerivedClass1 derivedClass1 = new DerivedClass1();
derivedClass1.item = baseType.item;
derivedClass1.itemGroup = baseType.itemGroup;
return derivedClass1;
}

Public DerivedClass2 SimpleTest2(BaseClass baseType)
{
DerivedClass2 derivedClass2 = new DerivedClass2();
derivedClass2.item = baseType.item;
derivedClass2.itemGroup = baseType.itemGroup;
return derivedClass2;
}

Public DerivedClass3 SimpleTest3(BaseClass baseType)
{
DerivedClass3 derivedClass3 = new DerivedClass3();
derivedClass3.item = baseType.item;
derivedClass3.itemGroup = baseType.itemGroup;
return derivedClass3;
}

The code written in all the 3 methods is the same! Is there a better way to achieve this, without code duplication? Is there a specific design pattern that I can apply here?

  • 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-05T21:28:03+00:00Added an answer on June 5, 2026 at 9:28 pm

    Use generics on this and you can express it fairly nicely:

    public T SimpleTest<T>(BaseClass baseType) where T : BaseClass, new()
    {
        T derived = new T();
        derived.item = baseType.item;
        derived.itemGroup = baseType.itemGroup;
        return derived;
    }
    

    There is one caveat that you have to have a default constructor or else this won’t work. In instances where you have a constructor with one or more arguments (but the same signature for each), you can dynamically instantiate the class using Reflection, as in dbaseman’s answer.

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

Sidebar

Related Questions

If I have a function which accepts more than one string parameter, the first
I want to write a function that accepts a parameter which can be either
I have to write functions which is setting pixel in WPF. I need to
If you have a C function which returns an integer, you could write a
I have a function that I use quite frequently, which allows me to write
I have a function that writes a bitmap string to a browser, which then
I'm trying to write a functions to modify strings in C. If I have
I'm trying to write a function that accepts a certain type or any of
Please suppose I have a function that accepts a pointer as a parameter. This
I am trying to port some C which uses read() and write() functions for

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.