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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:57:14+00:00 2026-06-02T18:57:14+00:00

My code is using a list with baseclasses that i later cast into their

  • 0

My code is using a list with baseclasses that i later cast into their respective derived types. I hit a point where I thought I could create a constructor for the baseclass that would figure out of what type it should be and change itself into that type. Something like this:

    public class BaseClass
    {
        // Constructor
        BaseClass(string Input)
        {
            if (Input.Substring(0, 5) == "Something")
                return new DerivedClass(); // <-- Not allowed
        }
    }
    public class DerivedClass : BaseClass
    {
    }

This isn’t allowed since a constructor must return void. Changing type of “this” doesn’t seem to be allowed either. I know that there are very simple alternatives to this code that works, but I think that it has a certain elegance to it, don’t you think?

  • 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-02T18:57:15+00:00Added an answer on June 2, 2026 at 6:57 pm

    You should look into the Factory Pattern. It is used when you want to create an instance of an object but allow the specific type to be determined based on other parameters.

    public static class BaseClassFactory
    {
        public static BaseClass Create(string input)
        {
            if (input.Substring(0, 5) == "Something")
                return new DerivedClass();
    
            return new BaseClass();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a cascading drop down list using JQuery. The code that I
In following code the issue is, that I cannot test dao.add() without using dao.list().size()
I'm generating List<int> x and List<int> y from List<Point> p using this code: List<int>
I am using List in C#. Code is as mentioned below: TestCase.cs public class
I m un-animating a list using this code... function unanimate_li() { $li.filter(':last') .animate({ height:
I wanted to generate a list view using below code. But after running this
Using following code I try to get updated list of checkbuttons' corresponding text values,
I'm using this code for get all news: List<aspnet_News> allNews = context.aspnet_News.OrderByDescending(i => i.NewsId).ToList();
I'm using this code: Dim cListItems As New System.Collections.Generic.List(Of Combobox_values) cListItems.Add(New Combobox_values(One, 1)) cListItems.Add(New
I have a nested oredered list which i m animating using this code... var

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.