2017-05-29 4 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

コードの画像を投稿しないでください。これは私たちがそれをテストすることは不可能です。コードは単なるテキストです。 – Steve

+0

Employeeクラスと質問に分割する文字列を追加してください –

+0

ファイルemployees.txtの少なくとも1行を入力してください。私は、あなたが 'temp [5] +"/"+ temp [6] +"/"+ temp [7]"のような連結の分割を必要とすると思います。 – PiLHA

答えて

1

"|"でテキストを分割しています。日付は「1995 | 5 | 22」形式になっています。これは、日付が3つに分割されることを意味します。最後の3つの項目(年、月、日)を取得した場合は、このような日付を設定できます。

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

こんにちは変更されるか? public DateTime dateOfBirth { を取得する { return _dateOfBirth; } セット { _dateOfBirth = value; } – rosie

+0

編集した回答を確認してください – Mertus

+0

ありがとうございました – rosie

関連する問題