我有一个班级预订,其中所有的信息来自2个其他类别的客户和沃图伊格.

我需要读取预留文本文件中的写入信息,并且我收到错误消息"INVALID INVALIZATE INITIALIZER MEMBER DECLARATOR".和"集合初始值设定项需要其类型‘Reserve’来实现System.Collections.IEnumerable."

这是我的预约课程:

   public class Reservation
    {
        public string fullpath = $@"{AppDomain.CurrentDomain.BaseDirectory}\Reservation.txt";

        private Customer _customer;
        private Voertuig _vehicle;

        public Customer Customers
        {
            get { return _customer ?? (_customer = new Customer()); }
            set { _customer = value; }
        }

        public Voertuig Vehicles
        {
            get { return _vehicle ?? (_vehicle = new Voertuig()); }
            set { _vehicle = value; }
        }
        public Reservation(): this(new Customer(), new Voertuig())
        {

        }

        public Reservation(Customer customer, Voertuig vehicle)
        {
            _customer = customer;
            _vehicle = vehicle;
        }

        public void Create()
        {
            try
            {
                if (File.Exists(fullpath))
                {
                    StreamWriter sw = new StreamWriter(fullpath, true);

                    var record = $"{Customers.Firstname};{Customers.Lastname};{Customers.Age};{Customers.Phonenumber};{Vehicles.Type};{Vehicles.Electric};{Vehicles.RequiredAge};{Vehicles.Wheels};{Vehicles.PersonsCanRide};{Vehicles.AllowedSpeed}";

                    sw.WriteLine(record);

                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (IOException ioex)
            {
                throw new Exception(ioex.Message);
            }
        }

        public List<Reservation> ReadFromCsv()
        {
            List<Reservation> _reservation = new List<Reservation>();

            try
            {

                if (File.Exists(fullpath))
                {

                    using (StreamReader reader = new StreamReader(fullpath))
                    {

                        string record = reader.ReadLine();

                        while (record != null)
                        {
                            string[] fields = record.Split(new char[] { ';' });

                            Reservation res = new Reservation
                            {
                                // error CS1922: Cannot initialize type 'Reservation' with a collection initializer
                                // because it does not implement 'System.Collections.IEnumerable'

                                // error CS0747: Invalid initializer member declarator
                                Customers.Firstname = fields[0],
                                Customers.Lastname = fields[1],
                            };
                            _reservation.Add(res);

                            record = reader.ReadLine();
                        }
                    }
                }
            }
            catch (IOException ioEx)
            {
                throw new Exception(ioEx.Message);
            }
            return _reservation;
        }
    }

我判断了这question个,但似乎不是同一个问题.

我是否需要在预订类中为所有客户和车辆属性创建属性?

推荐答案

您可以通过如下方式初始化您的保留类来解决该错误:

Reservation res = new Reservation
{
   Customers = new Customer
   {        
       Firstname = fields[0],
       Lastname = fields[1]
   }

};

初始值设定项旨在设置类内的属性,但您要做的是设置类内类(Customers类)的属性.

我在这里所做的是为Customer创建一个对象初始值设定项,然后将预留的Customers属性设置为新的Customer实例.

Csharp相关问答推荐

图形.DrawString奇怪异常的字距调整

错误401未授权ASP.NET Core.使用JWT创建身份验证/授权

以自动方式注销Azure身份应用程序

有没有一种方法可以在包含混合文本的标签中嵌入超链接?

Blazor. NET 8—阶段启动配置文件不启动网站VS2022

Quartz调度程序不调用作业(job)类

mocking对象的引发事件并作为用于调用方法的参数对象传递到那里

获取具有AutoFaces的所有IOptions对象的集合

Blazor在FluentButton onClick事件上设置参数

如何使用.NET6WPF打印车票?

泛型参数在.NET 8 AOT中没有匹配的批注

将FileStream的特定部分作为字节数组读取

在使用AWS SDK for.NET时,如何判断S3是否已验证我的对象的校验和?

使用未赋值的、传递的局部变量

如何将默认区域性更改为fr-FR而不是en-US?

C#中使用ReadOnlySpan的泛型类型推理

如何保存具有多个重叠图片框的图片框?

使用生产环境调试我的应用程序的快速方法

带有类约束的C#泛型

MS Project读取项目自定义域