我想知道如何将数组中的值传递并存储到C#中的类对象。将数组值传递给对象类
这里我主要程序代码
public partial class Form1 : Form
{
List<Configuration> lines = new List<Configuration>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
//Read in every line in the file
using (StreamReader reader = new StreamReader("file.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string textfile = line;
string[] array = new string[] { "\\n" };
string[] parts = textfile.Split(array, StringSplitOptions.RemoveEmptyEntries);
///////////////////////////////////////////////////
for (int i = 0; i < parts.Length; i++)
{
lines.Add(new Configuration(parts[i],0));
}
//lines.Add(new Configuration(line));
//listBox1.Items.Add(line);
}
}
listBox1.DataSource = lines;
listBox1.DisplayMember = "CompanyName";
}
}
我的类对象的代码
class Configuration
{
string _CompanyName;
int _Employees;
public Configuration(string companyname, int number_of_Employees)
{
_CompanyName = companyname;
_Employees = number_of_Employees;
}
//program properties and validation
public string CompanyName
{
set
{
_CompanyName = value;
}
get
{
return _CompanyName;
}
}// End of levelname validation
//program properties and validation
public int EmployeesNumber
{
set
{
_Employees = value;
}
get
{
return _Employees;
}
}// End of levelname validation
}
目前的程序读取其中有每个公司员工的公司的名单和电话号码的文本文件。像这样的结构
Microsoft\n92200
Google\n33077
Apple\n60400
IBM\n426751
Facebook\n3000
程序运行时,它将公司名称和员工数拆分为一个数组。该部分工作正常,它只是将所有内容存储到String公司名称中。当我尝试修改它以将值存储在类对象的不同字段中时出现错误。
“I an error” - 那个错误是? – 2012-04-21 07:04:18
注意:\ n是非常不寻常的分隔符,为什么不使用更像“,”或简单的空间的传统?除非你真的想通过使用通常代表“新行”字符作为列的单列行的读者来代替你的代码和配置的读者... – 2012-04-21 07:18:12