-2

I've got an essential skills program which is very simple. The question is within the code. I want the question to be loaded from a text file.

I have already added some questions to the text file.

I have a list index where a random question will be picked from the text file and it will load as a label.

The issue is when I try to load the question I get a: System.ArgumentOutOfRangeException: ''minValue' cannot be greater than maxValue. Parameter name: minValue' error

Here is my code:

        private void LoadQuestions()
    {
        if(Globals.intQuestionNumber == 11)
        {
            MessageBox.Show("Quiz complete - Redirecting", "Quiz Complete");

            var fileStream = new FileStream(@"H:\(4)Programming\Assignments\EssentialSkills - Optimised\EssentialSkills\Numeracy\QuestionsLvl0.txt", FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    stringAllInfo.Add(line);
                    MessageBox.Show(line);

                }
            }

            char[] delimiterChars = { ',' };

            foreach (string l in stringAllInfo)
            {
                string[] words = l.Split(delimiterChars);
                strQuestion.Add(words[0]);
                strAnswer.Add(words[1]);
                Console.WriteLine(words[0]);
                Console.WriteLine(words[1]);

            }


            Menus.Summary sum = new Menus.Summary();
            sum.Show();
            this.Hide();
        }

        else 
        {
            lblCountdownval.Visible = false;
            lblCountdown.Visible = false;

            Globals.intQuestionNumber += 0;
            lblQuestionsNumber.Text = "Question Number: " + Globals.intQuestionNumber.ToString();



            Random random = new Random();
            Globals.listIndex = random.Next(0, strAnswer.Count - 1);

            lblQuestion.Text = strQuestion.ElementAt(Globals.listIndex);

            Globals.listQuestonsAsked.Add(strQuestion.ElementAt(Globals.listIndex));
            btnCorrect.Text = strAnswer.ElementAt(Globals.listIndex).ToString();
            btnAnswer1.Text = random.Next(200).ToString();
            btnAnswer3.Text = random.Next(200).ToString();

            int locationIndex = random.Next(0, 3);
            btnCorrect.Location = points.ElementAt(locationIndex);

            locationIndex = random.Next(0, 3);

            btnAnswer1.Location = points.ElementAt(locationIndex);
            while ((btnAnswer1.Location == btnCorrect.Location))
            {
                locationIndex = random.Next(0, 3);
                btnAnswer1.Location = points.ElementAt(locationIndex);

            }
            locationIndex = random.Next(0, 3);
            btnAnswer3.Location = points.ElementAt(locationIndex);

            while ((btnAnswer3.Location == btnCorrect.Location) || (btnAnswer3.Location == btnAnswer1.Location))
            {
                locationIndex = random.Next(0, 3);
                btnAnswer3.Location = points.ElementAt(locationIndex);
            }

            btnAnswer1.Show();
            btnCorrect.BackColor = Color.White;
            btnAnswer3.Show();
        }

    }

and here are my Lists:

    public List<string> strQuestion = new List<string>();
    public List<string> strAnswer = new List<string>();
    public List<string> stringAllInfo = new List<string>();

I want the question from the text file to a label but I get that error.

Any advice?

Thanks.

Casper K
  • 3
  • 1
  • 2
    Steps to analyze your problem: 1) An exception (almost) always has a **stack trace** that tells you _exactly where_ the exception was thrown, so you know which code line to look at. 2) [debug](https://stackoverflow.com/q/25385173/5528593) your code step by step to see _why_ the exception occurs. My first guess: `strAnswer.Count` is 0 or less, hence `random.Next(0, strAnswer.Count - 1);` throws this exception. Why `strAnswer.Count` is 0, you can investigate by debugging. – René Vogt Feb 27 '20 at 15:21

1 Answers1

0

I assume you're falling down here

Globals.listIndex = random.Next(0, strAnswer.Count - 1);

Is strAnswer.Count 0? Check this by debugging. If not can you please identify which point causes the exception?

PeterT
  • 269
  • 1
  • 2
  • 9