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

  • Home
  • SEARCH
  • 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 6579421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:53:46+00:00 2026-05-25T15:53:46+00:00

I have come to a point in my program where my polymorphism is broken.

  • 0

I have come to a point in my program where my polymorphism is broken. I realize that this is in my code, and I understand why; I’m just not sure how best to resolve it.

I have three model classes which have the same interface, but very significantly different implementations. I created an interface, and then three independent classes, which also derive from a ModelBase class:

public interface IMyModel
{ ... }

public class MyModelA : ModelBase, IMyModels
{ ... }

public class MyModelB : ModelBase, IMyModels
{ ... }

public class MyModelC : ModelBase, IMyModels
{ ... }

So far fine and dandy.

I have a ViewModel base class, which takes a model as a constructor:

public abstract class MyViewModelBase
{
    public MyViewModelBase(ModelBase Model)
    this.model = Model;
}

Now where I am caught; I want to have a concrete ViewModel class that can accept any of the three Model classes above:

public class MyViewModel : MyViewModelBase
{
    MyViewModel(IMyModel Model) : base (Model)   // <- Invalid Polymorphism!
    {
        // More here
    }
}

This doesn’t work, because it is possible for an implementation of IMyModel to not be based on ModelBase. The argument cannot be safely passed to the base constructor.

I can see one solution being to create an abstract base class derived from ModelBase for these models with exception-throwing content, and using that as the type in my ViewModel. I had started with a base class, but found that almost every part had some difference! However, that seems like a lot of work. Also, it won’t ensure that derived classes implement everything (like an interface does). Finally, it seems to devalue the interface concept (indeed, I wouldn’t need one anymore).

I don’t see any way of marking the interface as saying that derived classes must have a specific base class. It would be nice if I could do this, but it’s not allowed:

public interface IMyModel : MyModelBase
{ ... }

Is there a better way to do this?

Clarification:

I probably oversimplified the names here. I have other Models and ViewModels using the base classes, but not implementing the interface.

public class MyOtherModel : ModelBase  // But not IMyModel!
{ ... }

public class MyOtherViewModel : MyViewModelBase
{
    MyOtherViewModel(MyOtherModel Model) : base(Model)  // This works
    { ... }
}
  • 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-25T15:53:46+00:00Added an answer on May 25, 2026 at 3:53 pm

    You could make your base class implement the interface, then inherit your implementation classes from the base class, marking the base class and methods as abstract (MustInherit/MustOverride in VB parlance). This would give you your polymorphism and guarantee the interface.

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

Sidebar

Related Questions

When designing an application, does there come a point where you have too many
I have come across a lot of optimization tips which say that you should
I have come across the following type of code many a times, and I
The entry point into a program in java is typically something like this //
I have encountered a problem in my program that has me somewhat stumped. I
I have a program that has Classes GUI Upload and a buffer between the
I have come to see an Installer class item in Visual studio. Why they
I have come across what must be a common problem. When I have an
I have come across a quirky feature in Visual Studio and I was interested
In my work I have come across syntax like global:: (C#) or G:Loading($value) in

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.