2017-05-29 34 views
-2

我想從一個文本文件中的數據爲出生日期的員工,我有在字符串中的所有其它信息出現在我的形式,但不DOB。以下是使用流讀取器獲取數據的代碼。獲取數據的形式C#

public bool Load(string employeesFile) 
     { 
     List<string> lines = new List<string>(); 

     using (StreamReader reader = new StreamReader("employees.txt")) 
     { 
      string line; 
      while ((line = reader.ReadLine()) != null) 
      { 
       //Splitting the data using | 
       string[] temp = line.Split('|'); 

       //This is to populate an employees detials 
       Employee emp = new Employee() 
       { 
        firstName = temp[0], 
        lastName = temp[1], 
        address = temp[2], 
        postCode = temp[3], 
        phoneNumber = temp[4], 
        //dateOfBirth = temp.ToString[5] 
       }; 

接下來是在表單中顯示數據的窗體中的代碼。

 public partial class Salaried_Employee_Details : Form 
    { 

    public Salaried_Employee_Details(Employee emp) 
    { 
     InitializeComponent(); 


     textBoxLastName.Text = emp.lastName; 
     textBoxFirstName.Text = emp.firstName; 
     textBoxAddress.Text = emp.address; 
     textBoxPostCode.Text = emp.postCode; 
     textBoxPhoneNumber.Text = emp.phoneNumber; 
     dateTimeDateOfBirth.Text = emp.dateOfBirth.ToString(); 

該文件中出生日期的格式爲1995 | 5 | 22。

我如何得到它從文本文件鏈接的形式展現?

+2

不要發佈您的代碼的圖像。這使我們無法測試它。代碼只是文本。只需將其添加到您的問題(IE複製/粘貼) – Steve

+0

添加您的員工類和你分裂的問題的字符串 –

+0

提供至少一行文件employees.txt。我認爲你需要像'temp [5] +「/」+ temp [6] +「/」+ temp [7]'這樣的連續分割。 – PiLHA

回答

1

您正在用「|」分割文本日期格式爲「1995 | 5 | 22」格式,這意味着您的日期將分爲三部分。如果你得到最後三項(年,月,日),你可以設置一個這樣的日期;

int year = Convert.Int32(temp[5]); 
int month = Convert.Int32(temp[6]); 
int day = Convert.Int32(temp[7]); 
//This is to populate an employees detials 
Employee emp = new Employee() 
{ 
    firstName = temp[0], 
    lastName = temp[1], 
    address = temp[2], 
    postCode = temp[3], 
    phoneNumber = temp[4], 
    dateOfBirth = new DateTime(year, month, day) 
}; 
+0

嗨所以將這個類必須被改變?公共DateTime的出生日期 { 得到 { 回報_dateOfBirth; } 設置 { _dateOfBirth =值; } – rosie

+0

請查看我編輯的答案 – Mertus

+0

工作過,謝謝 – rosie