T
T
tera10042019-02-18 19:54:46
C++ / C#
tera1004, 2019-02-18 19:54:46

Error in using collections in c#?

<code lang="cs">
namespace collection
{
    struct Payment
    {
        public int Petrol;
        public double Count;
        public DateTime Dt;
        public byte Column;
        public byte Code;
        public Payment(byte column, DateTime dt, int petrol, double count, byte code)

        {
            Column = column;
            Petrol = petrol;
            Count = count;
            Dt = dt;
            Code = code;

        }

        public String SString()
        {
            return String.Format("Колонка: {0} \n" +
                "Дата:  {1}.{2}.{3} {4}:{5}:{6} \n" +
                "Марка бензина: {7} \n" +
                "Количество: {8} \n" +
                "Код платежа: {9}" +
                "\n===============================", Column, Dt.Year, Dt.Month, Dt.Day, Dt.Hour, Dt.Minute, Dt.Second, Petrol, Count, Code);
        }



        // redaktor


      


    }
    public static new_list(List<Payment> payment)
    {

        Console.Write("Введите марку бензина: ");
        int another_petrol = int.Parse(Console.ReadLine());
        Console.Write("Введите количество бензина в литрах: ");
        double another_count = double.Parse(Console.ReadLine());
        Console.Write("Введите номер колонки: ");
        byte another_column = byte.Parse(Console.ReadLine());
        Console.Write("Введите дату: ");
        byte another_code = byte.Parse(Console.ReadLine());
        Console.Write("Введите код: ");
        DateTime another_dt = DateTime.Parse(Console.ReadLine());
        List<Payment> new_list1 = new List<Payment>() { new Payment(another_column, another_dt, another_petrol, another_count, another_code) };
        payment.Add(new_list1);
    }


    class Program
    { 
        public static void Menu() { 
            Console.WriteLine("1. История платежей");
            Console.WriteLine("2. Редактировать историю платежей");
            Console.WriteLine("3. Добавить новый платеж");
            Console.WriteLine("4. Удалить платеж из списка");
            Console.WriteLine("5. Поиск платежа по коду");
            Console.WriteLine("6. Выход из программы");
            Console.Write("\n \nВведите номер пункта меню: ");
            char M = char.Parse(Console.ReadLine());
                
                switch (M)
                {
                    case '1': break; // вывод списка платежей
                    case '2': break;
                    case '3':new_list();
                    break;
                    case '4': Console.WriteLine();
                        break;
                    case '5': break;
                    case '6': break;
                }
            
        }
        static void Main(string[] args)
        {

            Menu();
            Payment np = new Payment(1, new DateTime(2000, 12, 13,15,12,31),92,5.12,001);
            List<Payment> payments = new List<Payment>();
            payments.Add(np);
            payments.Add(new Payment(1, new DateTime(2012, 2, 12, 12, 42,21),92,5.13,001));
            foreach (Payment n in payments)
            {
                Console.WriteLine(np.SString());
            }
            
        }


    }


   

    
}   
</code>

Errors:
Line 62 - cs0116;cs0515;cs1520;cs0132
Line 76 - cs1503
Line 96 - cs0103

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2019-02-18
@FoggyFinder

Here you have several errors at once:
1. The new_list method does not belong to any particular type. I suspect it must be in class Program
2. The new_list method has no return type specified. I suspect you want the method to return Payment . In this case, it's better to rename it to ReadPayment :

public static Payment ReadPayment()
{
    Console.Write("Введите марку бензина: ");
    int another_petrol = int.Parse(Console.ReadLine());
    Console.Write("Введите количество бензина в литрах: ");
    double another_count = double.Parse(Console.ReadLine());
    Console.Write("Введите номер колонки: ");
    byte another_column = byte.Parse(Console.ReadLine());
    Console.Write("Введите дату: ");
    byte another_code = byte.Parse(Console.ReadLine());
    Console.Write("Введите код: ");
    DateTime another_dt = DateTime.Parse(Console.ReadLine());
    return 
        new Payment(another_column, another_dt, another_petrol, another_count, another_code);
}

3. In the Menu() method, calling new_list() does nothing, I assume you wanted to add a new element to the list:
case '3':
    payments.Add(ReadPayment());
    break;

Just in case, here is the whole code:
struct Payment
{
    public int Petrol;
    public double Count;
    public DateTime Dt;
    public byte Column;
    public byte Code;
    public Payment(byte column, DateTime dt, int petrol, double count, byte code)
    {
        Column = column;
        Petrol = petrol;
        Count = count;
        Dt = dt;
        Code = code;
    }

    public String SString()
    {
        return String.Format("Колонка: {0} \n" +
            "Дата:  {1}.{2}.{3} {4}:{5}:{6} \n" +
            "Марка бензина: {7} \n" +
            "Количество: {8} \n" +
            "Код платежа: {9}" +
            "\n===============================", Column, Dt.Year, Dt.Month, Dt.Day, Dt.Hour, Dt.Minute, Dt.Second, Petrol, Count, Code);
    }
}
class Program
{
    public static Payment ReadPayment()
    {
        Console.Write("Введите марку бензина: ");
        int another_petrol = int.Parse(Console.ReadLine());
        Console.Write("Введите количество бензина в литрах: ");
        double another_count = double.Parse(Console.ReadLine());
        Console.Write("Введите номер колонки: ");
        byte another_column = byte.Parse(Console.ReadLine());
        Console.Write("Введите дату: ");
        byte another_code = byte.Parse(Console.ReadLine());
        Console.Write("Введите код: ");
        DateTime another_dt = DateTime.Parse(Console.ReadLine());
        return
            new Payment(another_column, another_dt, another_petrol, another_count, another_code);
    }
    public static List<Payment> payments = new List<Payment>();
    public static void Menu()
    {
        Console.WriteLine("1. История платежей");
        Console.WriteLine("2. Редактировать историю платежей");
        Console.WriteLine("3. Добавить новый платеж");
        Console.WriteLine("4. Удалить платеж из списка");
        Console.WriteLine("5. Поиск платежа по коду");
        Console.WriteLine("6. Выход из программы");
        Console.Write("\n \nВведите номер пункта меню: ");
        char M = char.Parse(Console.ReadLine());

        switch (M)
        {
            case '1': break; // вывод списка платежей
            case '2': break;
            case '3':
                payments.Add(ReadPayment());
                break;
            case '4':
                Console.WriteLine();
                break;
            case '5': break;
            case '6': break;
        }
    }
    public static void Main(string[] args)
    {
        Menu();
        Payment np = new Payment(1, new DateTime(2000, 12, 13, 15, 12, 31), 92, 5.12, 001);
        payments.Add(np);
        payments.Add(new Payment(1, new DateTime(2012, 2, 12, 12, 42, 21), 92, 5.13, 001));
        foreach (Payment n in payments)
            Console.WriteLine(np.SString());
    }
}

A few comments:
1. Instead of the SString method, you can override the standard .ToString()
method 2. It is better to take out the response to user input from the Menu method, leaving only the printing itself to the console there.
3. It is better to make the structure explicitly immutable through readonly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question