For some reason the ‘removeitems’ function is causing the following error:
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
Does anyone know why? I have no idea. Thanks for any help
int num = 0;
List<string> questionList = new List<string>() { "q1", "q2", "q3"};
List<string> choices = new List<string>() { "correct1", "c2", "c3", "c4" };
List<string> answers = new List<string>() { "correct1", "correct2", "correct3", "correct4" };
public void removeitems() {
questionList.RemoveAt(num);
choices.RemoveAt(num);
choices.RemoveAt(num + 1);
choices.RemoveAt(num + 2);
choices.RemoveAt(num + 3);
answers.RemoveAt(num);
}
Well, the exception also gives you the line number it happens on, so that’d help you to pinpoint the exact problem.
Secondly, keep track of how many objects are left in the choices list while removing items. After you’ve removed the first item, how many items are left in the choices list? Then think about whether “num + 3” really makes sense after you’ve already removed the first 3 items.
I’d also would try to store your question into an extra object, like question with its answer(s), so you can iterate through it or do whatever you want with it. Besides that, it looks like you want to rather clear the choices choices.Clear(); then remove every single item by itself.
And, as @BlackPete pointed out, keep track of what you doing. If you remove at 0, your 0 + 3 won’t work anymore, as your last index is now 2 (0,1,2)