我有一个班级预订,其中所有的信息来自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个,但似乎不是同一个问题.
我是否需要在预订类中为所有客户和车辆属性创建属性?