繁体   English   中英

尝试将文本文件读入类,然后循环遍历

[英]Trying to read a text file into classes then cycle through

大家好,我是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;
    }
}

任何人有任何想法要去哪里错了吗? 我没有错误,但是发现文本字段不会随着新记录的更新,然后当逐步通过数组类保存正确数量的记录和字段时,我感觉它将非常明显地使我无法动弹。

任何帮助将不胜感激。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM