При добавлении циклом нескольких элементов student в List у всех у них становится одинаковым поле оценки - C#
Формулировка задачи:
Здравствуйте.
Помогите пожалуйста, у меня возникла проблема с добавлением элемента в List.
Лист состоит из элементов типа student(Поля student - имя, номер группы, оценки)
При добавлении циклом нескольких элементов student у всех у них становится одинаковым поле оценки, при этом когда я вывожу на экран этот элемент student перед тем как добавить его в лист, то вижу что перед добавлением оценки у всех разные.
У меня 3 файла и 1 текстовый документ: program.cs, MyList.cs, student.cs и List1.txt
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyList l1 = new MyList();
l1.fout();
Console.ReadKey();
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
public class MyList : List<student>
{
public MyList()
{
}
public void writes()
{
foreach (student s in this)
{
s.WriteSt();
}
}
public void fwrite()
{
foreach (student s in this)
{
StreamWriter sw = new StreamWriter(@"C:\documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\List1.txt");
sw.WriteLine("{0}, {1}, {2} {3} {4} {5} {6}", s.Name, s.Group, s.Sc[0], s.Sc[1], s.Sc[2], s.Sc[3], s.Sc[4]);
sw.Close();
}
}
public void fout()
{
int[] arr = new int[5];
arr[0] = 0;
arr[1] = 0;
arr[2] = 0;
arr[3] = 0;
arr[4] = 0;
StreamReader reader = File.OpenText(@"C:\documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\List1.txt");
{
string line = null;
do
{
line = reader.ReadLine();
if (line != null)
{
student s = new student("", 0, arr);
s = s.ScanSt(line);
s.WriteSt();
this.Add(s);
}
} while (line != null);
}
this.writes();
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class student
{
private string _name;
private int _group;
private int[] _sc = new int[5];
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Group
{
get { return _group; }
set { _group = value; }
}
public int[] Sc
{
get { return _sc; }
set { _sc = value; }
}
public student(string name, int group, int[] sc)
{
this._name = name;
this._group = group;
this._sc = sc;
}
public student ScanSt(string st)
{
int i = 0;
{
while (i <= st.Length)
{
if (st[i] == ',')
{
i += 1;
break;
}
else
{
this._name = this._name + st[i];
i += 1;
}
}
string g1 = null;
i += 1;
while (i <= st.Length)
{
if (st[i] == ',')
{
i += 1;
break;
}
else
{
g1 = g1 + st[i];
this._group = Convert.ToInt32(g1);
i += 1;
}
}
this._sc[0] = Convert.ToInt32(st[i + 1]) - 48;
this._sc[1] = Convert.ToInt32(st[i + 3]) - 48;
this._sc[2] = Convert.ToInt32(st[i + 5]) - 48;
this._sc[3] = Convert.ToInt32(st[i + 7]) - 48;
this._sc[4] = Convert.ToInt32(st[i + 9]) - 48;
}
return this;
}
public void WriteSt()
{
Console.WriteLine("Имя:{0}, Номер группы: {1}, Оценки: {2} {3} {4} {5} {6}", Name, Group, Sc[0], Sc[1], Sc[2], Sc[3], Sc[4]);
}
}
}
abcd1, 1231, 4 5 3 5 4
abcd2, 1232, 3 3 3 3 3
abcd3, 1233, 4 4 4 4 4
abcd4, 1234, 5 5 4 4 5
abcd5, 1235, 5 5 5 5 5
abcd6, 1236, 3 4 4 3 4
abcd7, 1237, 4 4 3 4 4
abcd8, 1238, 4 3 4 3 4
abcd9, 1239, 5 4 5 4 5
abcd0, 1230, 5 5 4 5 5
Заранее спасибо
Решение задачи: «При добавлении циклом нескольких элементов student в List у всех у них становится одинаковым поле оценки»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyList myStudents = new MyList();
myStudents.FillList();
foreach (Student st in myStudents)
{
Console.WriteLine(st.ToString());
}
Console.ReadLine();
}
}
public class MyList:IEnumerable<Student>
{
private List<Student> myStudents;
public MyList()
{
myStudents = new List<Student>();
}
public void FillList()
{
using (StreamReader sr = File.OpenText(@"C:\myList.txt"))
{
while (sr.Peek() != -1)
{
string[] temp = sr.ReadLine().Split(new char[]{','});
string[] sc = temp[2].Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
int[] intSc = (from n in sc select int.Parse(n)).ToArray();
myStudents.Add(new Student(temp[0], int.Parse(temp[1]), intSc));
}
}
}
public IEnumerator<Student> GetEnumerator()
{
return myStudents.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return myStudents.GetEnumerator();
}
}
public class Student
{
public string Name { get; set; }
public int Group { get; set; }
private int[] Sc = new int[5];
public Student(string name,int group,int[] sc)
{
Name = name;
Group = group;
Sc = sc;
}
public override string ToString()
{
return string.Format("Имя: {0}; Номер группы: {1}; Оценки: {2}, {3}, {4}, {5}, {6}",
Name, Group, Sc[0], Sc[1], Sc[2], Sc[3], Sc[4]);
}
}
}