2016-04-11 38 views
-1

下面的代碼適用於顯示在我的日曆中有圖標的假期的人,但是我遇到的問題是我的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; 
} 

也是他們一個整潔的方式做我覺得我的代碼是非常臃腫。

+0

我也將整個if/elseif的東西移動到字典中,然後直接訪問它。這包含重複的代碼 –

回答

0

您確定這些是導致您的問題的線?因爲如果_list至少包含一個元素,則只能輸入foreach循環的正文。因此,_list.Count >= 1將始終爲真,並且i將遞增,並且永不會變爲-1

我懷疑你綁定的例外來自這行代碼

List<Resource> resources = new List<Resource>(apertureAppointments.Resources.GetResourcesByType("Managers")); 
Resource res = resources[5]; 

確定,總有在此列表中至少有6個項目發起的呢?

一般來說,如果你用foreach循環遍歷它們,爲什麼按索引訪問你的列表項?你可以很容易地做到這一點如下

foreach (tblApertureNetShiftPattern sp in _list) { 

    DateTime? dt1 = sp.startDate; 
    DateTime? dt2 = sp.endDate; 
} 

就像你與manager_name屬性做到這一點。

如果你堅持按索引訪問,我建議在循環結束時遞增索引變量i。通過這種方式,在訪問數組之前,並不總是必須減小索引。

+0

是的,他們永遠是6管理人員暫時的程序 – rogue39nin

+0

如果這是真的,你的發佈代碼是沒有辦法的,那就會出現一個超出界限的例外。 – derpirscher

+0

那麼,OP說:「當我沒有在[數組INT] -1 ...」。顯然刪除'-1'會導致出界。那麼真正的問題是,爲什麼首先有'i ++'? –

相關問題