2017-02-08 102 views
0

我想給一個成員變量(該變量是一個List)分配一個List,但我總是得到錯誤信息:System.StackOverflowException未處理,我不知道爲什麼,請參閱我的代碼如下:爲成員變量賦值失敗,發生StackOverflowException

public class Employee 
{ 
    public int EmployeeID { get; set; } 
    public string EmployeeName { get; set; } 
    public int DeptID { get; set; } 
} 

public class Department 
{ 
    public int DeptID { get; set; } 
    public string DeptName { get; set; } 
    public List<Employee> Employees 
    { 
     set { Employees = value; } 
     //get { return EmployeeDataAccessLayer.getEmployeesByDeptID(DeptID); } 
     get { return Employees; } 
    } 
} 
public class EmployeeDataAccessLayer 
{ 
    public static List<Employee> getEmployeesByDeptID(int deptID) 
    { 
     string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
     string sqlStr = "select * from tblemployee where deptid = :deptid order by employeeid asc"; 
     List<Employee> employees = new List<Employee>(); 
     using (OracleConnection ocon = new OracleConnection(conStr)) 
     { 
      OracleCommand ocmd = new OracleCommand(sqlStr, ocon); 
      ocmd.Parameters.Add(":deptid", deptID); 
      ocmd.Connection.Open(); 
      OracleDataReader rdr = ocmd.ExecuteReader(); 
      while (rdr.Read()) 
      { 
       Employee employee = new Employee(); 
       employee.EmployeeID = Convert.ToInt32(rdr["EMPLOYEEID"]); 
       employee.EmployeeName = rdr["NAME"].ToString(); 
       employee.DeptID = Convert.ToInt32(rdr["DEPTID"]); 
       employees.Add(employee); 
      } 
     } 
     return employees; 
    } 
} 

public class DepartmentDataAccessLayer 
{ 

    public static List<Department> getAllDepartments() 
    { 
     string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
     string sqlStr = "select * from tbldepartment order by departmentid asc"; 
     List<Department> depts = new List<Department>(); 

     using (OracleConnection ocon = new OracleConnection(conStr)) 
     { 
      OracleCommand ocmd = new OracleCommand(sqlStr, ocon); 
      ocmd.Connection.Open(); 
      OracleDataReader rdr = ocmd.ExecuteReader(); 
      while (rdr.Read()) 
      { 
       Department dept = new Department(); 
       dept.DeptID = Convert.ToInt32(rdr["DEPARTMENTID"]); 
       dept.DeptName = rdr["NAME"].ToString(); 
       dept.Employees = EmployeeDataAccessLayer.getEmployeesByDeptID(dept.DeptID); 
       depts.Add(dept); 
      } 
     } 

     return depts; 
    } 
} 

所以,我調試了一下,發現了異常出現在:

set { Employees = value;}  

見下圖:

enter image description here

我真的不知道爲什麼,你能告訴我原因還是給我一些建議?

回答

1

因爲你設置Employeesvalue被設置Employeesvalue被設置Employeesvalue這是設置.......等等,直到堆棧溢出。

因爲你不使用私有變量保存的員工,你可以只使用一個自動的getter和-setter的值設置爲自己,喜歡你的其他屬性:

public List<Employee> Employees { get; set; } 

否則,設置一個私人變量來存放員工名單。這可以使用

private List<Employee> _employees; 
public List<Employee> Employees 
{ 
    set { _employees = value; } 
    get { return _employees; } 
} 
1

如果您正在設置在C#中的值使用get和set,所有你需要做的是

public List<Employee> Employees{get; set;} 

隨着空get和set塊,值將被自動分配。在代碼中,你基本上一遍又一遍的呼喚你的變量設定的功能,直到StackOverflow的例外發生在性能here

2

在你的情況

更多的信息,當你分配的東西給Employees它的setter將再次觸發它分配給相同的變量,所以相同的過程將繼續,它會導致無限的分配,這就是爲什麼你得到這個例外。爲了克服這一變化的屬性定義是這樣的:

private List<Employee> _Employees 
public List<Employee> Employees 
{ 
    set { _Employees = value; }  
    get { return _Employees; } 
} 

或像這樣:

public List<Employee> Employees { get; set; }