各項目を取得して 1 つずつ表示する必要がある IEnumerable があります。表示は継続的なプロセスではありません。つまり、1 つのアイテムを取得して UI に表示し、そのアイテムに対するユーザーのフィードバックを待ってから、次のアイテムに移動する必要があります。たとえば、以下のコードでは、質問を取得してユーザーに表示し、ユーザーが Enter キーを押して、次の質問の取得に進む必要があります。
私の質問はどうすればいいですか?IEnumerable はこれを達成するための最良の方法ですか、それともリストに戻ってインデックスの保存を開始し、1 つずつインクリメントする必要がありますか?
.NET 3.5 を使用していることに注意してください。
コード:
class Program
{
static void Main(string[] args)
{
Exam exam1 = new Exam()
{
Questions = new List<Question>
{
new Question("question1"),
new Question("question2"),
new Question("question3")
}
};
var wizardStepService = new WizardStepService(exam1);
var question = wizardStepService.GetNextQuestion();
//Should output question1
Console.WriteLine(question.Content);
Console.ReadLine();
//Should output question2 but outputs question1
question = wizardStepService.GetNextQuestion();
Console.WriteLine(question.Content);
Console.ReadLine();
//Should output question3 but outputs question1
question = wizardStepService.GetNextQuestion();
Console.WriteLine(question.Content);
Console.ReadLine();
}
}
public class Question
{
private readonly string _text;
public Question(string text)
{
_text = text;
}
public string Content { get { return _text; } }
}
internal class Exam
{
public IEnumerable<Question> Questions { get; set; }
}
internal class WizardStepService
{
private readonly Exam _exam;
public WizardStepService(Exam exam)
{
_exam = exam;
}
public Question GetNextQuestion()
{
foreach (var question in _exam.Questions)
{
//This always returns the first item.How do I navigate to next
//item when GetNextQuestion is called the second time?
return question;
}
//should have a return type hence this or else not required.
return null;
}
}