使用文件c#中的vector属性读取对象

时间:2018-04-18 10:05:47

标签: c# object vector

我有这堂课:

public class Student
{
private int[] note;//vector de note
        private DateTime dataInrolarii;
        private int nrRestante; 
        private readonly int nrMatricol; 
        private const string sex = "feminin"
        public static int anStudiu = 3; 

 public Student(string cnp, string nume, int nrMatricol, DateTime dataInrolarii, int nrRestante, int[] note) : base(cnp, nume)
        {
            this.nrMatricol = nrMatricol;
            this.dataInrolarii = Convert.ToDateTime(dataInrolarii);
            this.nrRestante = nrRestante;
            this.note = new int[note.Length];
            for (int i = 0; i < note.Length; i++)
                this.note[i] = note[i];
        }
}

当然,我有getter,setter,toString()重载等等。 我有一个包含此对象的文本文件:

2920118080023, George, 2, 12/09/2016, 4, 10 3 4

我想从文件中读取并在文本框中显示来自文件的学生。但是我真的不知道怎么用矢量音符来做这个。

这是我写的:

    private void button_CitireTXT_Click(object sender, EventArgs e)
    {

        StreamReader sr = new StreamReader("citiretxt.txt");
        string linie = null;
        while ((linie = sr.ReadLine()) != null)
        {
            try
            {//oricare ar fi tipul unui atribut citit din fisier, ele sunt STRING acolo
                string cnp = linie.Trim().Split(',')[0];
                string nume = linie.Trim().Split(',')[1];
                int nrmat = Convert.ToInt32(linie.Trim().Split(',')[2]);
                string datainrolString = linie.Trim().Split(',')[3];
                DateTime datainrol = DateTime.Parse(datainrolString);
                int nrRest = Convert.ToInt32(linie.Trim().Split(',')[4]);

                string noteString = linie.Split(' ')[5];

                for(int i=0;i< noteString.Length;i++)
                {
                    noteString[i] = Convert.ToInt32(noteString[i]);

                string[] notesString
                }



                Student s = new Student(cnp, nume, nrmat, datainrol, nrRest, note);
                tb_CitireDinTxt.Text += s.ToString() + Environment.NewLine;

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

如何从文件中读取矢量注?谢谢

更新:

private void button_CitireTXT_Click(object sender,EventArgs e)         {

        StreamReader sr = new StreamReader("citiretxt.txt");
        string linie = null;
        while ((linie = sr.ReadLine()) != null)
        {
            try
            {
                string cnp = linie.Trim().Split(',')[0];
                string nume = linie.Trim().Split(',')[1];
                int nrmat = Int32.Parse(linie.Trim().Split(',')[2]);
                string datainrolString = linie.Trim().Split(',')[3];
                DateTime datainrol = DateTime.Parse(datainrolString);
                int nrRest = Int32.Parse(linie.Trim().Split(',')[4]);

               int[] notes = Array.ConvertAll(linie.Split(',')[5].Split(' '), int.Parse);


                Student s1 = new Student(cnp, nume, nrmat, datainrol, nrRest, notes);

                tb_CitireDinTxt.Text += s1.ToString() + Environment.NewLine;

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

通过考虑写作答案,Vector(来自C ++) - &gt;列表(在C#中)。

如果以下是您的文件内容,

2920118080023, George, 2, 12/09/2016, 4, 10 3 4

,拆分。那么你在第5个索引处的字符串将包含10 3 4。现在将其转换为整数使用Array.ConvertAll()的数组,或者也可以将其转换为List

尝试下面的代码

//Converting into int array
int[] notes = Array.ConvertAll(linie.Split(',')[5].Split(' '), int.Parse)

//Converting into List<int>
List<int> notes= Array.ConvertAll(linie.Split(',')[5].Split(' '), int.Parse).ToList()

而不是

string noteString = linie.Split(' ')[5];

for(int i=0;i< noteString.Length;i++)
     {
          noteString[i] = Convert.ToInt32(noteString[i]);

          string[] notesString
     }