There are a few things you need to change to start with.
Firstly, stop using
ref
everywhere - you don't need it for the questions collection, and it's misleading for your integers.
Secondly, stop re-inventing the wheel and use the "built in" features of the framework.
So, delete the GetNextArrayIndex method completely, and change the questionList to a
List<MultipleChoiceQuestion>
instead.
You can then use Add to add a question to the end, without having to think about what it's index might be:
private static MultipleChoiceQuestion CreateNewQuestion()
{
... Read your question details here ...
return new MultipleChoiceQuestion(question, choice1, choice2, choice3, choice4, correctChoice);
}
Then when you use it, you add it to your collection:
case 1:
questionList.Add(CreateNewQuestion());
break;
That way, the method has only one thing to do - create a question - without having to know or think about what the rest of the code is going to do with it. This is called
separation of concerns and it makes your code cleaner, easier to read and a lot more reliable / maintainable.
Do the same thing with out other methods, and then change the way
TakeQuiz
works:
public static void TakeQuiz(List<MultipleChoiceQuestion> questions, List<MultipleChoiceQuestion> correct, List<MultipleChoiceQuestion> incorrect)
{
...
}
Inside the method, you can just use
foreach
to get each question in turn (without worrying about it's index) and if he gets it right it goes in the
correct
collection, otherwise
incorrect
.
The calling code can then tell the number of correct and incorrect answers by using the collections Count property, and can iterate through the wrong ones with another foreach on the incorrect collection.