Which I’m trying to accomplish is have two classes, a non generic and a generic.
I need a non generic class because I plan to insert objects of this class and insert them in a List<T>
Here’s the code
// Non-generic
public class Response
{
public object Value { get; }
}
// Generic
public class Response<T> : Response
{
public T Value { get; set; }
}
I’d like to have a List<Response>, where when I access to this object I get the Value property as object.
But when I receive this object as generic, access to the T Value property and hide the object Value property.
I hope be clear, if not.. please let me know.
EDIT: This is for a quiz. So, each question has answers.. For example in MultipleChoiceQuestion, it could have several answers A, B, C, D as view shapes, or could be strings, or integers.
public abstract class Question
{
public Question(string questionText)
{
this.QuestionText = questionText;
}
public string QuestionText { get; set; }
}
// Non-generic
public class Response
{
public object Value { get; }
}
// Generic
public class Response<T> : Response
{
public T Value { get; set; }
}
public class MathProblemQuestion : Question
{
public Response Response { get; set; }
}
public class MultipleChoiseQuestion : Question
{
public Response Response { get; set; }
public IEnumerable<Response> PossibleResponses;
...
}
public class TrueOrFalse : Question
{
...
}
I would personally just give them different names. It will make your life much simpler, and the code much clearer:
Note that I’ve made the
Responseclass abstract – it’s hard to see how it could work elegantly otherwise; ifResponsehas its own storage forValuethen presumably that could be set to a non-Tvalue. Hopefully making this abstract won’t be a problem for you.