2013-02-22 4 views
0

안녕하세요 꽤 초보자 C#을 오면 텍스트 파일을 읽고 수업을 통해 섹션으로 나누지 만 어디에 선언해야하는지에 문제가 있습니다. 그렇다면 기록을 어떻게 순환 할 것인가. 여기 어디 잘못되어 가고 있어요 어떤 아이디어가다음 클래스를 통해 텍스트 파일을 읽으려고 시도

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Assignment_3 
{ 
    class student 
    { 
     public string firstname; 
     public string middlename; 
     public string surname; 
     public DateTime dob; 
     public string sex; 
    } 
} 

사람이 내 코드

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Collections; 
using System.Windows.Forms; 

namespace Assignment_3 
{ 
    public partial class Form1 : Form 
    { 
     string s; 
     string ss; 
     int i = 1; 
     string infilename; 
     int num; 
     SortedList sList = new SortedList(); 
     int x = 0; 

     public Form1() 
     { 
      InitializeComponent(); 
      student myself = new student(); 
      infilename = "text.txt"; 
      StreamReader sr1 = new StreamReader(infilename); 
      sList.Clear(); 
      while ((s = sr1.ReadLine()) != null) 
      { 
       string[] strs = s.Split(','); 

       myself.firstname = strs[0]; 
       myself.middlename = strs[1]; 
       myself.surname = strs[2]; 
       myself.dob = DateTime.Parse(strs[3]); 
       myself.dob.ToString(strs[3]); 
       myself.sex = strs[4]; 
       ss = myself.dob.ToString("u"); 
       sList.Add(myself.firstname, myself); 

      } 
      sr1.Close(); 
      num = sList.Count; 
      student[] pArray = new student[num]; 
      string[] keys = new string[num]; 



      foreach (DictionaryEntry d in sList) 
      { 
       keys[x] = (string)d.Key; 
       pArray[x] = (student)d.Value; 
       x++; 
      } 
      if (i == 0) 
      {lblmessage.Text = "Already at the first record."; i = 1; } 
      if (i == num) 
      {lblmessage.Text = "Already at the last record.";i = num-1; } 

      lbllastname.Text = pArray[i].surname; 
      lblfirstname.Text = pArray[i].firstname; 
      lblsecondname.Text = pArray[i].middlename; 
      lbldob.Text = pArray[i].dob.ToString(); 
      lblsex.Text = pArray[i].sex; 

     } 

     private void btnlast_Click(object sender, EventArgs e) 
     { 
      i = num; 

     } 
     private void btnfirst_Click(object sender, EventArgs e) 
     { 
      i = 0; 
     } 

     private void btnnext_Click(object sender, EventArgs e) 
     { 
      i++; 

     } 

     private void btnprev_Click(object sender, EventArgs e) 
     { 
      i--; 
     } 


    } 
} 

내 클래스 파일입니다 ?? 나는 오류가 없지만 텍스트 필드가 새 레코드로 업데이트되지 않는다는 것을 알았습니다. 그리고 배열 클래스를 밟았을 때 정확한 양의 레코드와 필드를 보유하고있을 때 내 손가락을 그 위에 올려 놓은 것 같은 느낌이 들었습니다. .

도움이 될 것입니다.

+0

어떤 오류가 발생했는지 알려주지 않았습니까? –

+0

while 루프에서 읽는 모든 행에 대해 새 학생을 만듭니다. myself = new student(); – rene

답변

0

루프 안에 student의 새 인스턴스를 만들어야합니다. 왜냐하면 지금은 학생 수업의 인스턴스가 하나 뿐이므로 SortedList의 모든 항목이 같은 개체를 가리키고 있기 때문입니다.

관련 문제