【发布时间】:2018-12-05 13:16:30
【问题描述】:
我正在我的项目中创建一个驾驶执照对象,所有员工都将拥有一个与他们唯一的时钟编号相关联的驾驶执照对象。我的数据库中有一个单独的表格用于驾驶执照,但将来需要添加更多类型的车辆,有没有在不重新编码的情况下做到这一点? 我数据库中的列与下面类的属性相同
public class LicenseDTO
{
public int ClockNo { get; set; }
public bool CBalance { get; set; }
public bool MR16 { get; set; }
public bool OrderPicker { get; set; }
public bool Reach { get; set; }
public bool Pedestrian { get; set; }
public bool Lorry { get; set; }
public bool Sweeper { get; set; }
public bool Washer { get; set; }
}
编辑
我已经尽力做到最好,但我觉得它真的很啰嗦,可以用更有效的方式来完成。这是我的代码的更新版本。
public class LicenseDTO
{
public int ClockNo { get; set; }
public List<Common.VehicleTypeDTO> Vehicles { get; set; }
}
public class VehicleTypeDTO
{
public string VehicleType { get; set; }
public bool Allowed { get; set; }
}
private void btnClockCardIn_Click(object sender, EventArgs e)
{
Common.LicenseDTO License = new Common.LicenseDTO();
List<Common.VehicleTypeDTO> Vehicles = new List<Common.VehicleTypeDTO>();
Common.VehicleTypeDTO CBalance = new Common.VehicleTypeDTO();
Common.VehicleTypeDTO MR16 = new Common.VehicleTypeDTO();
License.Vehicles = Vehicles;
CBalance.VehicleType = "CBalance";
CBalance.Allowed = true;
MR16.VehicleType = "MR16";
MR16.Allowed = false;
License.Vehicles.Add(CBalance);
License.Vehicles.Add(MR16);
foreach (Common.VehicleTypeDTO Vehicle in License.Vehicles)
{
MessageBox.Show(Vehicle.VehicleType + " " + Vehicle.Allowed);
}
}
【问题讨论】:
-
不重新编码?可能是通过 xml 文件,但为什么呢?
-
基本上我已经参加了一年的实习,也是最后一个实习的学生,所以没有人会维护这些应用程序,以防万一
-
而不是定义“车辆”列表,而是为每个带有类型标识符的许可证收集车辆。
-
不要太拘泥于试图保持你的代码简短。冗长不是犯罪。我看不出你的编辑有什么问题。它似乎做你想做的事。如果您不顾一切,请在创建时内联您的 VehicleTypeDTO 属性设置:
new Common.VehicleTypeDTO() { VehicleType = "CBalance", Allowed = true};。 -
好的,谢谢你的帮助:)
标签: c# sql-server winforms