我有我的代碼循環使用,但它顯示使用未分配的本地變量的錯誤。使用未分配的本地變量
這裏是我的代碼:
public List<CustomTypeLayer.EmployeeLeave> GetLeaveRecord(int emplid)
{
DataSet ds = objLMSRepository.GetLeaveRecord(emplid);
List<EmployeeLeave> leavelist = new List<EmployeeLeave>();
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
for (int k; k < ds.Tables[0].Rows.Count; k++)
{
DataRow dr = ds.Tables[0].Rows[k];
EmployeeLeave leave = new EmployeeLeave();
leave.DateFrom = Convert.ToDateTime(dr["dt_from"]);
leave.DateTo = Convert.ToDateTime(dr["dt_to"]);
leave.EmployeeId = Convert.ToInt32(dr["EmployeeID"]);
leave.Reason = Convert.ToString(dr["txt_reason"]);
leave.LeaveType = (LeaveReason)Enum.Parse(typeof(LeaveReason), dr["id_leave_type"].ToString(), true);
leavelist.Add(leave);
}
}
return leavelist;
}
的錯誤是在for循環中k
。你可以看到for循環我已經初始化k
與int k
。
add int k = 0;好吧 – MMK