我正在从需要放入对象数组(myEmployees)的文件中读取数据。我相信我的代码在本示例结束之前是正确的,但我不确定如何从文件中读取数据,将其拆分,然后将其正确放入我的类对象数组中。
//declare an array of employees
Employee[] myEmployees = new Employee[10];
//declare other variables
string inputLine;
string EmpName;
int EmpNum;
double EmpWage;
double EmpHours;
string EmpAdd;
//declare filepath
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "\\";
//get input
Console.Write("\nEnter a file name in My Documents: ");
string input = Console.ReadLine();
string path = environment + input;
Console.WriteLine("Opening the file...");
//read file
StreamReader myFile = new StreamReader(path);
inputLine = (myFile.ReadLine());
所以我从一个结构如下的文件中读取数据:
Employee Number
Employee Name
Employee Address
Employee wage Employee Hours
我需要从此文件中读取数据并将其解析到我创建的员工数组中。这是 Employee 类的类数据:
public void Employeeconst ()
{
employeeNum = 0;
name = "";
address = "";
wage = 0.0;
hours = 0.0;
}
public void SetEmployeeNum(int a)
{
employeeNum = a;
}
public void SetName(string a)
{
name = a;
}
public void SetAddress(string a)
{
address = a;
}
public void SetWage(double a)
{
wage = a;
}
public void SetHours(double a)
{
hours = a;
}
public int GetEmployeeNum()
{
return employeeNum;
}
public string GetName()
{
return name;
}
public string GetAddress()
{
return address;
}
public double GetWage()
{
return wage;
}
public double GetHours()
{
return hours;
}