2015-01-27 60 views
1

我想獲取併發送此對象的列表到文本文件。該文本文件格式如下。集拋出StackOverflowException C#

name,IDnumber,department,value

有,所以我使用的用於閱讀。 這是閱讀代碼和寫入文件的這一頗有幾行。

public List<Employee> ReadFile(string fileName) { 
     StreamReader fileIn = new StreamReader(fileName); 
     fileIn = File.OpenText(fileName); 
     List<Employee> list = new List<Employee>(); 
     string[] test; 
     string name; 
     string ID; 
     string dep; 
     string post; 

     while (!fileIn.EndOfStream || !File.Exists(fileName)) { 

      string inString = fileIn.ReadLine(); 
      test = inString.Split('#'); 
      name = test[0]; 
      ID = test[1]; 
      dep = test[2]; 
      post = test[3]; 
      Employee newEmp = new Employee(name, ID, dep, post); 
      list.Add(newEmp); 

     } 
     fileIn.Close(); 
     return list; 
    } 

public void WriteFile(List<Employee> outList, string file) { 

     StreamWriter writeOut = new StreamWriter(file); 

     for (int i = 0; i < outList.Count; i++) { 

      writeOut.WriteLine(outList[i].name + '#' + outList[i].IDnum + '#' + outList[i].department + '#' + outList[i].position); 

     } 
     writeOut.close(); 
    } 

這是我的類的代碼。錯誤正在被拋出。

public class Employee { 

    public string name { get { return name; } set { name = value; } } 
    public string IDnum { get { return IDnum; } set { IDnum = value; } } 
    public string department { get { return department; } set { department = value; } } 
    public string position { get { return position; } set { position = value; } } 

    public Employee() { 

     name = string.Empty; 
     IDnum = string.Empty; 
     department = string.Empty; 
     position = string.Empty; 

    } 

    public Employee(string newName, string newID) { 

     name = newName; 
     IDnum = newID; 
     department = string.Empty; 
     position = string.Empty; 

    } 

    public Employee(string newName, string newID, string newDep, string 
    newPost) { 

     name = newName; 
     IDnum = newID; 
     department = newPost; 
     position = newDep; 

    } 

} 

我不確定是否有某種格式,我缺少set函數根據需要運行。這是我要求進出文件的功能。我相信它永遠不會出來,所以我可能會導入數據。

回答

5

這是一個非常常見的問題...通道的C#儀式!

讓我們來看看一個屬性(這適用於所有的屬性雖然) ...

public string name { get { return name; } set { name = value; } } 

所以會發生什麼,當你嘗試myObj.name = "foo";

set方法中,您可以返回到同一個屬性name。所以它試圖訪問name,它會再次出現(再次,並且再次遞歸直到您的StackOverflow)。

與適當的命名慣例支持字段是這裏的常態:

private string name; 
public string Name{get { return name; } set{ name = value; }} 

,甚至更好,如果沒有涉及到邏輯,auto-property

public string Name{ get; set; } 
+0

<3謝謝先生。 – 2015-01-27 02:10:22

2

您保持通話IDNUM 和其他屬性遍地遞歸,直到堆棧溢出

public string IDnum { get { return IDnum; } set { IDnum = value; } } 

當你做這樣的事情

IDnum = someValue; 

調用setter方法IDnum,它運行設置器中的代碼

IDnum =值

然後調用IDnum的setter,直到您用完堆棧。

的修復

在你的情況下,它看起來像你可以使用自動屬性

public string IDnum { get; set; } 
+0

那麼,語法應該如何呢?它應該只是 '獲得{返回IDNum;'和 '集{IDnum =值;' – 2015-01-27 01:35:04

+0

++,並且其他屬性具有相同的問題。 – 2015-01-27 01:35:16

+0

非常感謝!我以前遇到過自動麻煩,但看起來像我糾正了我的錯誤造成的。 – 2015-01-27 01:45:52

0

你應該改變

public string name { get { return name; } set { name = value; } } 
public string IDnum { get { return IDnum; } set { IDnum = value; } } 
public string department { get { return department; } set { department = value; } } 
public string position { get { return position; } set { position = value; } } 

public string name { get; set; } 
public string IDnum { get; set; } 
public string department { get; set; } 
public string position { get; set; } 

或引進後備字段:

private string _name; 
public string name { get { return _name; } set { _name = value; } } 

有關在C#自動實現的屬性的詳細信息,請參閱https://msdn.microsoft.com/en-us/library/bb384054.aspx

請注意,公共財產的常用命名是PascalCasing。您在PascalCasing中的屬性如下所示:

public string Name { get; set; } 
public string IdNum { get; set; } 
public string Department { get; set; } 
public string Position { get; set; }