Is there any way to make each class that implements interface Ae has a List of one of the concrete classes that implement Ba as property?
public interface IQuestion
{
IAnswerOption[] Answers { get; set; }
}
public interface IAnswerOption
{
int Id { get; }
bool IsCorrect { get; set; }
}
public class AnswerOptionText : IAnswerOption
{
public int Id { get; }
public bool isCorrect;
public string ansText;
}
public class AnswerOptionImage : IAnswerOption
{
public int Id { get; }
public bool isCorrect;
public string imgSlug;
}
public class AudioQuestion : IQuestion
{
public AnswerOptionImage[] Answers;
public string audioName;
}
public class TextQuestion : IQuestion
{
public AnswerOptionText[] Answers { get; set; }
public string questionText { get; set; }
}
When I try it, AudioQuestion and TextQuestion doesn't allow me to use AnswerOptionImage[] and AnswerOptionText[] respectively.
Visual Studio says that I need to implement interface member IQuestion.Answers, but this is not what I intend.
If someone can help me I would be very grateful. Thanks.
It seems that using generics for your IQuestion
interface will be a good fit:
public interface IQuestion<T> where T: IAnswerOption
{
T[] Answers { get; set; }
}
public class AudioQuestion : IQuestion<AnswerOptionImage>
{
public AnswerOptionImage[] Answers{ get; set; }
public string audioName;
}
public class TextQuestion : IQuestion<AnswerOptionText>
{
public AnswerOptionText[] Answers { get; set; }
public string questionText { get; set; }
}