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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:56:39+00:00 2026-05-28T05:56:39+00:00

I’ve got problem using generics. I’m creating an interface called IProblem , where each

  • 0

I’ve got problem using generics. I’m creating an interface called IProblem, where each problem has results (answers) and a result (if it is correct)

public interface IProblem<T>
{
    ushort ResultCount { get; }
    T[] Results { get; }

    bool IsCorrect();
}

public abstract class ProblemBase<T> : IProblem<T>
{
    private T[] _results;
    private ushort? _resultCount;

    public ushort ResultCount
    {
        get
        {
            if (_resultCount == null) throw new ArgumentNullException("_resultCount");
            return (ushort)_resultCount;
        }
        protected set
        {
            if (_resultCount != value)
                _resultCount = value;
        }
    }

    public T[] Results
    {
        get
        {
            if (_results == null)
                _results = new T[ResultCount];

            return _results;
        }
    }

    public abstract bool IsCorrect();
}

This is an example where I create an arithmetic problem, called ProblemA. T is decimal because the array datatype should be decimal (anothers problems maybe might have string, or int)

public class ProblemA: ProblemBase<decimal>
{
    private decimal _number1;
    private decimal _number2;
    private Operators _operator;

    public decimal Number1
    {
        get { return _number1; }
        set { _number1 = value; }
    }

    public decimal Number2
    {
        get { return _number2; }
        set { _number2 = value; }
    }

    public Operators Operator
    {
        get { return _operator; }
        set { _operator = value; }
    }

    public decimal Result
    {
        get { return Results[0]; }
        set { Results[0] = value; }
    }

    public ProblemA()
    {
        this.ResultCount = 1;
    }

    public override bool IsCorrect()
    {
        bool result;

        switch (_operator)
        {
            case Operators.Addition:
                result = this.Result == (this.Number1 + this.Number2);
                break;
            case Operators.Subtract:
                result = this.Result == (this.Number1 - this.Number2);
                break;
            case Operators.Multiplication:
                result = this.Result == (this.Number1 * this.Number2);
                break;
            case Operators.Division:
                result = this.Result == (this.Number1 / this.Number2);
                break;
            default:
                throw new ArgumentException("_operator");
        }

        return result;
    }
}

I’m using MVVM, so I’d like to have a ViewModel for each problem where contains ProblemBase<T> as property, but how it’s a generic, I guess it will be a problem if a put in IProblemViewModel as generic.

public interface IProblemViewModel : IViewModel
{
    ProblemBase<T> Problem { get; set; }
}

I said this because later a plan to use a ObservableCollection<IProblemViewModel>, so I’m not sure if there’s no problem if I write IProblemViewModel or IProblemViewModel<T>.
Thanks in advance.

  • 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-28T05:56:40+00:00Added an answer on May 28, 2026 at 5:56 am

    Maybe I haven’t understood this perfectly, but is this what you are after?

        ObservableCollection<IProblemViewModel<object>> collection = new ObservableCollection<IProblemViewModel<object>>
        {
            new ProblemViewModel<DerivedResult>(),
            new ProblemViewModel<OtherResult>()
        };
    

    This can be achieved by declaring the generic argument as covariant.

    You could also change the collection to

    ObservableCollection<IProblem<BaseType>>
    

    and just have it accept a specific result chain. In this example, DerivedResult and OtherResult must then inherit from BaseType to fit into the collection.

    The big caveat is that primitive types don’t fit into this hierarchy, in any way. You will have to wrap them in IProblem<IntResult> and so on.

    Of course, you could implement a simple carrier, for example Boxer which would box any value type instead of implementing one for each type.

    One last caveat: It’s not possible to have a ‘set’ property on a covariant type, so IProblemViewModel can only support get.

    A complete, compilable example:

    class Program
    {
    
        public interface IProblem<out T>
        {
            ushort ResultCount { get; }
            T[] Results { get; }
    
            bool IsCorrect();
        }
    
        public class ProblemBase<T> : IProblem<T>
        {
            private T[] _results;
            private ushort? _resultCount;
    
            public ushort ResultCount
            {
                get
                {
                    if (_resultCount == null) throw new ArgumentNullException("_resultCount");
                    return (ushort)_resultCount;
                }
                protected set
                {
                    if (_resultCount != value)
                        _resultCount = value;
                }
            }
    
            public T[] Results
            {
                get
                {
                    if (_results == null)
                        _results = new T[ResultCount];
    
                    return _results;
                }
            }
    
            public bool IsCorrect()
            {
                return true;
            }
        }
    
        public interface IProblemViewModel<out T>
        {
            IProblem<T> Problem { get; }
        }
    
        public class BaseResult
        {
    
        }
    
        public class DerivedResult : BaseResult
        {
    
        }
    
        public class OtherResult : BaseResult
        {
    
        }
    
        public class ProblemViewModel<T> : IProblemViewModel<T>
        {
    
            public IProblem<T> Problem
            {
                get
                {
                    throw new NotImplementedException();
                }
                set
                {
                    throw new NotImplementedException();
                }
            }
        }
    
    
        static void Main(string[] args)
        {
            ObservableCollection<IProblemViewModel<object>> collection = new ObservableCollection<IProblemViewModel<object>>
            {
                new ProblemViewModel<DerivedResult>(),
                new ProblemViewModel<OtherResult>()
                //, new ProblemViewModel<int>()   // This is not possible, does not compile.
            };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.