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 companyName に格納するだけです。クラスオブジェクトの異なるフィールドに値を保存するように変更しようとすると、エラーが発生します。