我想知道如何將數組中的值傳遞並存儲到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