Error entering data from the keyboard.

Mistake

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleClassConlsole { class Program { static void Main(string[] args) { ReadProductsArray(); Console.ReadKey(); } public static int n { get; private set; } public static Product[] products = new Product[n]; public static Product[] Products { set; get; } public static void ReadProductsArray() { Console.WriteLine("Введіть кількість товарів:"); if (!byte.TryParse(Console.ReadLine(), out byte n)) { Console.WriteLine("Помилка введення значення. Будь-ласка повторіть введення значення ще раз!"); } for (int i = 0; i < n; i++) { Console.WriteLine($"----------------{i}---------------"); Console.WriteLine("Введіть ім'я:"); products[i].Name = Console.ReadLine(); Console.WriteLine("Введіть ціну:"); products[i].Price = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("Введіть кількість:"); products[i].Quantity = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Введіть виробника:"); products[i].Producer = Console.ReadLine(); Console.WriteLine("Введіть вагу:"); products[i].Weight = Convert.ToSingle(Console.ReadLine()); } } public static void PrintProducts(Product[] products) { for (int i = 0; i < n; i++) { Console.WriteLine($"------------------------------------\n" + $"Товар #{i}: {products[i].Name}\n Ціна: {products[i].Price}\n " + $"Кількість: {products[i].Quantity}\n Виробник: {products[i].Producer}\n " + $"Вага: {products[i].Weight}\n"); } } public static void PrintProduct(Product product) { Console.WriteLine($"------------------------------------\n" + $"Товар #: {product.Name}\n Ціна: {product.Price}\n " + $"Кількість: {product.Quantity}\n Виробник: {product.Producer}\n " + $"Вага: {product.Weight}\n"); } public static void GetProductsInfo(out Product lower, out Product high) { high = products[0]; lower = products[0]; float lowCost = products[0].Price, hightCost = products[0].Price; for (int i = 0; i < n; i++) { if (products[i].Price > hightCost) { hightCost = products[i].Price; high = products[i]; } if (products[i].Price < lowCost) { lowCost = products[i].Price; lower = products[i]; } } } } } 

    2 answers 2

    Lines:

     public static int n { get; private set; } public static Product[] products = new Product[n]; 

    Executed before the value n is set in the ReadProductsArray method. Therefore, at the time of the call

     public static Product[] products = new Product[n]; 

    The value of n will be 0 and an array of 0 elements will be created.

    Because of this, any call inside the loop later will cause an error.

    As a solution, you must either create an array after setting n , or use a list and not refer to the index, for example:

     public static int n { get; private set; } public static List<Product> products = new List<Product>(); public static void ReadProductsArray(){ ... for (int i = 0; i < n; i++) { var product = new Product(); ... product.Name = Console.ReadLine(); ... products.Add(product); } ... } 
        public static void ReadProductsArray() { Console.WriteLine("Введіть кількість товарів:"); while (!int.TryParse(Console.ReadLine(), out n)) { Console.WriteLine("Помилка введення значення. Будь-ласка повторіть введення значення ще раз!"); } products = new Product[n]; // создать массив после того, как стало известно n for (int i = 0; i < n; i++) { products[i] = new Product(); // создать products[i] ...