0

I am using a for loop with a declared INT based on count of items in a list. The list I am populating is dynamic based on file list in a directory. The logic should read that if the count is equal or over 1k records, load 1k records at a time, otherwise if it is lower, count the items in list and set that as a max for the for loop. However the for loop only runs 499 times and errors(image). THIS ERROR IMAGE.

Anybody know how to solve the error occurring?

Below is the CORE of the code to make it easy to follow.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Linq;

namespace ViewProductImages
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }
             
                //new list
        List<string> filenames = new List<string>();          
       
        private void txtDirectory_TextChanged(object sender, EventArgs e)
        {
            
                //clear list - added peace of mind
            filenames.Clear();   

           
               //list is populated here via Directory.Getfiles


                //Sorted here kinda
                filenames.Sort();



        }
                 //Declare INT for for loop

        int maxImagesToShow;

        private void button1_Click(object sender, EventArgs e)
        {

                //Setting value for INT - IF over 1k records, set to 1k, IF under - set to actual list item count

            if (filenames.Count() >= 1000)
            {
                maxImagesToShow = 1000;
            }
            else
            {
                maxImagesToShow = filenames.Count();
            }

            try
            {

                //for loop - loops based on value in INT

                for (int i = 0; i < maxImagesToShow; i++)
                {                  
                    var filename = filenames[i];   

                   

                //remove current item being processed from the list(filenames)

                    filenames.Remove(filename);


                }

            }
            catch (Exception ex)
            {
                
            }
        }
    }
}
  • 2
    Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Yong Shun May 16 '21 at 06:30
  • 2
    Hint: suppose you have 5 items in the list, so you're going to iterate from 0-4. Then you call `Remove` twice... how many items are in the list? What is element 4 after you've removed the first two? (Why remove the items at all within the loop? I would suggest using List.RemoveRange...) – Jon Skeet May 16 '21 at 06:34
  • Please remember to include error messages and their stack traces as text in your question. Even from the dialog in the screenshot, you should have been able to press CTRL+C and copy it. – ProgrammingLlama May 16 '21 at 06:40

1 Answers1

1

You are reducing the size of the list by removing items from it, but the for loop maintains the original condition and expects to see up to 1000 items. There won't be 1000 items in the list at the end of the loop if you remove them. You will also be skipping items as when you remove from the start of the list you decrement the index of all the others.

Tom W
  • 5,108
  • 4
  • 30
  • 52