나는 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
같은 구조는 때 프로그램은 따로 배열로 직원의 회사 이름과 번호를 분할 실행합니다. 그 부분은 잘 작동하고, 그것은 문자열 companyName에 모든 것을 저장합니다. 클래스 개체의 다른 필드에 값을 저장하도록 수정하려고 시도하면 오류가 발생합니다.
"나는 오류"- 그 오류는 무엇입니까? –
사이드 노트 : \ n은 매우 특이한 구분 기호입니다. ","또는 단순히 공백과 같은 더 전통적인 것을 사용하지 않으시겠습니까? 만약 당신이 정말로 코드와 구성의 독자들을 하나의 행에서 열의 separtor로서 "new line"문자를 나타내는 것을 사용하여 conuse하고 싶지 않다면 ... –