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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:44:04+00:00 2026-05-24T17:44:04+00:00

All my classes are derived from this T class: public abstract class Problem<T, TResult>

  • 0

All my classes are derived from this T class:

public abstract class Problem<T, TResult> : IEquatable<T>
{
    protected Problem()
    {
        Results = new TResult[ResultCount];
    }

    protected Problem(int problemNumber, int subject, int seconds) : this()
    {
        this.ProblemNumber = problemNumber;
        this.Subject = subject;
        this.Seconds = seconds;
    }

    public int ProblemNumber { get; set; }
    public int Subject { get; set; }
    public int Seconds { get; set; }
    public abstract int ResultCount { get; }
    public TResult[] Results { get; set; }
    public abstract bool IsCorrect { get; }
    protected abstract bool CheckTheAnswer(params TResult[] results);
    public abstract bool Equals(T other);
}

I need to have a list of them, but List ask for a definitive datatype for T and TResult. Well, not necessary a list.

I wouldn’t like to use object datatype an cast each one (because I’d use a super large switch case all the time).

UPDATE 1:

One of my classes is this one:

public class Comparison2 : Problem<Comparison2, Comparators>
{
    public Comparison2(decimal number1, decimal number2) : base()
    {
        this.SetNumbers(number1, number2);
    }

    public Comparison2(decimal number1, decimal number2, int problemNumber, int subject, int seconds)
        : base(problemNumber, subject, seconds)
    {
        this.SetNumbers(number1, number2);
    }

    private void SetNumbers(decimal number1, decimal number2)
    {
        this.Number1 = number1;
        this.Number2 = number2;
    }

    public decimal Number1
    {
        get;
        set;
    }

    public decimal Number2
    {
        get;
        set;
    }

    public override int ResultCount
    {
        get { return 1; }
    }

    public override bool IsCorrect
    {
        get { return this.CheckTheAnswer(Results[0]); }
    }

    protected override bool CheckTheAnswer(params Comparators[] results)
    {
        if (results.Length != ResultCount)
            throw new ArgumentException("Only expected " + ResultCount + " arguments.");

        Comparators result = results[0];

        switch (result)
        {
            case Comparators.Minor:
                return Number1 < Number2;
            case Comparators.Major:
                return Number1 > Number2;
            case Comparators.Equal:
                return Number1 == Number2;
            case Comparators.None:
                return false;
            default:
                throw new Exception("Comparator unexpected");
        }
    }

    public override bool Equals(Comparison2 other)
    {
        if (other == null)
            return false;

        return this.Number1 == other.Number1 && Number2 == other.Number2;
    }
}

I plan to put all of them in an List<> and extract each one their properties directly.

  • 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-24T17:44:05+00:00Added an answer on May 24, 2026 at 5:44 pm

    As discussed in the comments, it is hard to tell how to improve your design without the whole picture. It might be, that you would have to create a separate class for every concrete type T, something like IntegerProblem, FractionProblem etc., but I am not sure.

    Part of your question is concern for large switch statements. In your update, you have possibly unnecessary switch statement, so I think if I explain you how to get rid of such switches, you might avoid many of them in other places.

    It is considered a bad habit if you switch on a type or a state of an object. It generally means that some other object knows too much about (is tightly coupled to) this object. Instead of asking an object about its state and acting according to the answer, you should ask the object itself to act depending on its own state.

    This happens in your CheckTheAnswer method. An instance of Comparison2 asks an instance of Comparators what is its state and then does some work depending on it. Instead, you should ask an instance of Comparators to check Number1 and Number2. So, you need to add an abstract method to the Comparators class (you can name it e.g. “check”) that returns bool and takes two decimal arguments. Then create subclasses of Comparators for each of the states (e.g. MinorComparators, MajorComparators and NoneComparators) and override the abstract method (“check”) in them so that each subclass returns the same result as your switch.

    This way you don’t have to write large switches and can add new types of Comparators without having to change any of your Problem subclasses.

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

Sidebar

Related Questions

I have multiple classes that all derive from a base class, now some of
Suppose we have an abstract class Element from which classes Triangle and Quadrilateral are
I have an abstract class CommandPath, and a number of derived classes as below:
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have two classes, Person and Company, derived from another class Contact. They are
I have a stateless, abstract base class from which various concrete classes inherit. Some
I have some functionality that I need in all my classes which derive from
C# doesn't allow structs to derive from classes, but all ValueTypes derive from Object.
I have several classes, that all derives from SuperClass. When the classes are created,
In C# you can easily read all classes from a given assembly. I'm looking

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.