2015-01-27 61 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; }