我正在从需要放入我的对象数组(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;
}
@Robert Harvey什么是删除我的答案的原因。请让我知道,这样我就不会重复这个错误。 – Sandeep 2012-04-16 01:49:14