下面的代碼適用於顯示在我的日曆中有圖標的假期的人,但是我遇到的問題是我的for語句中的第一行代碼,其中提取日期時間對象超出返回的自定義列表
protected void apertureAppointments_TimeSlotCreated(object sender, TimeSlotCreatedEventArgs e)
int i = 0;
bool isFound = false;
List<tblApertureNetShiftPattern> _list = new List<tblApertureNetShiftPattern>();
_list = _dal.getHolidays();
List<Resource> resources = new List<Resource>(apertureAppointments.Resources.GetResourcesByType("Managers"));
Resource res = resources[5];
foreach (tblApertureNetShiftPattern sp in _list)
{
if (_list.Count >= 1)
i++;
else
i = 0;
DateTime? dt1 = _list[i - 1].startdate;
DateTime? dt2 = _list[i - 1].endDate;
if (e.TimeSlot.Start == dt1 && e.TimeSlot.Resource.Text == sp.manager_name)
{
isFound = true;
if (DoDateRangesOverlap(e.TimeSlot.Start, e.TimeSlot.End, dt1, dt2) && isFound == true)
{
Label temperatureLabel = new Label();
if (sp.appointmentType == Constants.shiftDayoff)
{
e.TimeSlot.CssClass = "DayOfCssStyle";
temperatureLabel.CssClass = "DayOfCssStyle";
}
else if (sp.appointmentType == Constants.shiftHoliday)
{
e.TimeSlot.CssClass = "HolidayCssStyle";
temperatureLabel.CssClass = "HolidayCssStyle";
}
else if (sp.appointmentType == Constants.shiftStat)
{
e.TimeSlot.CssClass = "statCssStyle";
temperatureLabel.CssClass = "statCssStyle";
}
else if (sp.appointmentType == Constants.shiftsickDay)
{
e.TimeSlot.CssClass = "SickDayStyle";
temperatureLabel.CssClass = "SickDayStyle";
}
temperatureLabel.Text = sp.Description;
Image imageControl = new Image();
imageControl.ImageUrl = @"~\images\aperturenet\Calendar\resources\holidays.png";
temperatureLabel.BackColor = System.Drawing.Color.Orange;
dt1 = null;
dt2 = null;
isFound = false;
e.TimeSlot.Control.Controls.AddAt(1, temperatureLabel);
e.TimeSlot.Control.Controls.AddAt(2, imageControl);
}
}
}
我的問題就出在這裏
if (_list.Count >= 1) i++; else i = 0; DateTime? dt1 = _list[i - 1].startdate; DateTime? dt2 = _list[i - 1].endDate;
此代碼時,我沒有-1在[陣列INT]它的炸彈,因爲OBV列表中顯示0,1和循環公關obally顯示2項。什麼是避免對象超出範圍索引錯誤的最佳方法?
Getholidays是一個簡單的列表,當工作人員記住我們在這裏沒有什麼好看的時候。
public List<tblApertureNetShiftPattern> getHolidays()
{
List<tblApertureNetShiftPattern> list = new List<tblApertureNetShiftPattern>();
var q = from _holidays in apertureNetEntities.tblApertureNetShiftPatterns.Where(w => w.isDeleted == false)
select _holidays;
list = q.ToList();
return list;
}
也是他們一個整潔的方式做我覺得我的代碼是非常臃腫。
我也將整個if/elseif的東西移動到字典中,然後直接訪問它。這包含重複的代碼 –