T
T
Tatami48652022-01-30 17:27:45
C++ / C#
Tatami4865, 2022-01-30 17:27:45

How to correctly populate lists of objects from another class and extract data from them?

There are 2 lists in the ImageCompressor class, the first one is filled by pressing a button on the main form, and the second one (hidden from the user), inside the ImageCompressor class, after calling the Compress() method.

When I try to pull data from these lists, the program crashes (the exception is associated with an index outside the list - it appears due to the emptiness of the second list).

In debug mode, you can see that the second list inside the ImageCompressor class is filled with elements, and when you try to get data from it through the main form, it is empty, why?

Class "ImageCompressor"

public class ImageCompressor {
        public readonly string FilePath;
        public readonly string FileName;
        public readonly long FileSize;

        // ОБЬЯВЛЕНИЕ СПИСКОВ
        public List<ImageCompressor> FilesBeforeProcessing { get; set; }
        public List<ImageCompressor> FilesAfterProcessing { get; private set; }

        public ImageCompressor() {
            // ИНИЦИАЛИЗАЦИЯ СПИСКОВ
            FilesBeforeProcessing = new List<ImageCompressor>();
            FilesAfterProcessing = new List<ImageCompressor>();
        }

        public ImageCompressor(string path) {
            FilePath = path;
            FileName = new FileInfo(path).Name;
            FileSize = new FileInfo(path).Length;
        }

        // МЕТОД ЗАПОЛНЯЮЩИЙ ВТОРОЙ СПИСОК ЭЛЕМЕНТАМИ
        public void Fill(string outputpath) {
            int length = FilesBeforeProcessing.Count;

            for (int i = 0; i < length; i++) {
                FilesAfterProcessing.Add(new ImageCompressor(
                    Path.Combine(outputpath, FilesBeforeProcessing[i].FileName)));
            }
        }
    }


Main_Form

private void button6_Click(object sender, EventArgs e) {
            ImageCompressor compressor = new ImageCompressor();

            // ПЕРВЫЙ СПИСОК ЗАПОЛНЯЕТСЯ ЭЛЕМЕНТАМИ
            compressor.FilesBeforeProcessing.AddRange(new List<ImageCompressor> {
                new ImageCompressor(@"C:\Users\MSI\Desktop\Translator\uno.jpg"),
                new ImageCompressor(@"C:\Users\MSI\Desktop\Translator\ч.jpg"),
            });

            // ВТОРОЙ СПИСОК ЗАПОЛНЯЕТСЯ ЭЛЕМЕНТАМИ
            compressor.Fill(@"C:\Users\MSI\Desktop\Translator");

            int length = compressor.FilesBeforeProcessing.Count;

            // ОТОБРАЗИТЬ ПЕРВЫЙ СПИСОК - ЗАПОЛНЕН ЭЛЕМЕНТАМИ
            for (int i = 0; i < length; i++) {
                listBox1.Items.Add($"| Name Before {compressor.FilesBeforeProcessing[i].FileName}");
                listBox1.Items.Add($"| Size Before {compressor.FilesBeforeProcessing[i].FileSize}");
            }

            // ОТОБРАЗИТЬ ВТОРОЙ СПИСОК - ПОЧЕМУ-ТО ПУСТОЙ
            for (int i = 0; i < length; i++) {
                listBox1.Items.Add($"| Name After {compressor.FilesAfterProcessing[i].FileName}");
                listBox1.Items.Add($"| Size After {compressor.FilesAfterProcessing[i].FileSize}");
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oleg_ods, 2022-01-30
@oleg_ods

public ImageCompressor(string path) {
            // Здесь должна быть инициализация списков или вызов конструктора без параметров 
            FilePath = path;
            FileName = new FileInfo(path).Name;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question