2015-10-16 25 views
0

完全错误:C# - 列表<string>'不包含定义‘物品’

System.Collections.Generic.List<string>' does not contain a definition for 'Items' and no extension method 'Items' accepting a first argument of type 'System.Collections.Generic.List<string>' could be found (are you missing a using directive or an assembly reference?)

我的任务是用savefiledialog窗口保存为文本文件,这是我使用的代码:

public void Create() 
     { 
      SaveFileDialog save = new SaveFileDialog(); 

      save.FileName = "Report.txt"; 

      save.Filter = "Text File | *.txt"; 

      if (save.ShowDialog() == DialogResult.OK) 
      { 

       StreamWriter writer = new StreamWriter(save.OpenFile()); 

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

        writer.WriteLine(_Reports.Items[i].ToString()); 

       } 

       writer.Dispose(); 
       writer.Close(); 

      } 
     } 

我也初始化使用

List<string> _Reports = new List<string>(); 

的清单,但清单的项目方法是不t支持。有没有办法来解决这个问题?

+2

这应该只是'_Reports.Count',项目是一个用VB财产。你在for循环中有同样的问题,它应该是'_Reports [i] .ToString()',删除Items属性。 –

+0

@RonBeyer哦!,谢谢!现在完美运作。 – Katherine

回答

4

没有“Items”属性。你可以阅读有关List类here

这将只是_Reports.Count

+0

非常感谢! – Katherine

0

你的错误说明了一切。没有什么所谓的项目与List<string>所以,你的代码应该是

public void Create() 
{ 
    SaveFileDialog save = new SaveFileDialog(); 
    save.FileName = "Report.txt"; 
    save.Filter = "Text File | *.txt"; 
    if (save.ShowDialog() == DialogResult.OK) 
    { 
     StreamWriter writer = new StreamWriter(save.OpenFile()); 
     for (int i = 0; i < _Reports.Count; i++) 
     { 
      writer.WriteLine(_Reports[i].ToString()); 
     } 
     writer.Dispose(); 
     writer.Close(); 
    } 
} 
相关问题