0

嗨,当涉及到 c# 时,我是一个相当新手,我一直在尝试读出一个文本文件,然后将其拆分为带有类的部分,但是在声明它们的位置以及如何循环浏览记录方面遇到了麻烦。这是我的代码

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--;
        }


    }
}

和我的班级文件

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;
    }
}

有人知道哪里出错了吗?我没有错误,但发现文本字段没有用新记录更新,然后当单步通过数组类保存正确数量的记录和字段时,我觉得这将是一件非常明显的事情,我不能把它放在上面.

任何帮助将不胜感激。

4

1 回答 1

0

您应该创建student内部for循环的新实例。因为目前您只有一个学生类实例,并且其中的所有项目SortedList都指向同一个对象。

于 2013-02-22T19:24:09.677 回答