首页 > 解决方案 > 如何从异常处理中排除当前选定的项目

问题描述

我对 C# 比较陌生,目前正在处理一个我想从异常中排除已编辑项目的项目。但是,在这种情况下,用户可以编辑包括约会在内的约会;不能有重叠的约会。当我运行我的程序并编辑约会时,例外是检查不应该编辑的约会。有人可以建议我如何做到这一点吗?

foreach (var appt in AppointmentScreen.ListOfAppts)
{                    
    if (selectedStart <= appt.Start && selectedEnd > appt.Start && (!(SelectApptID >= 0)) || SelectApptID >= 0)
    {
        overlaping = true;
    }

    if (appt.Start <= selectedStart && appt.End > selectedStart && (!(SelectApptID >= 0)) || SelectApptID >= 0)
    {
        overlaping = true;
    }
}

如果需要更多信息,请告诉我。我将全天定期检查。

感谢您提前提供的所有帮助

标签: c#

解决方案


正在编辑的约会是否有一个唯一的 ID,您可以使用它进行检查?

如果不是,如何将 Guid 属性添加到约会对象并使用简单的条件跳过(如果它是当前的)?就像是:

foreach (var appt in AppointmentScreen.ListOfAppts)
{
    if (appt.Id == CurrentAppt.Id) continue;  //this condition skips the appointment being edited
    if (selectedStart <= appt.Start && selectedEnd > appt.Start && (!(SelectApptID >= 0)) || SelectApptID >= 0)
    {
        overlaping = true;
    }

    if (appt.Start <= selectedStart && appt.End > selectedStart && ((SelectApptID >= 0)) || SelectApptID >= 0)
    {
        overlaping = true;
    }
}

在您的约会课程中:

public class Appointment 
{
    public Guid Id { get; set; }
    .
    .        
    .
}

推荐阅读