在类c中初始化数组#

时间:2016-09-24 13:38:12

标签: c#

有一种方法可以从文件中读取数据

        public static void ReadData(out StudentMarks[] Students, out int amount)
    {
        amount = 0;
        Students = new StudentMarks[Max];
        using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P3\\P3.2\\StudentsMarks.csv"))
        {
            reader.ReadLine(); reader.ReadLine();
            string line = null;
            int[] marks;
            marks = new int[Max];
            while (null != (line = reader.ReadLine()))
            {
                string[] values = line.Split(',');
                string surname = values[0];
                string name = values[1];
                string group = values[2];
                int amountOfMarks = int.Parse(values[3]);
                int i = 0; int yMax = 3 + amountOfMarks;int yMin = 4;
                while (amountOfMarks >= i)          
                {
                    if (yMin <= yMax)
                    {
                        marks[i] = int.Parse(values[yMin]);
                        yMin++;
                    }
                    i++;
                }
            StudentMarks MarksObj = new StudentMarks(surname, name, group, amountOfMarks, marks);
            Students[amount++] = MarksObj;
            }
        }
    }

有一个课程:

class StudentMarks
{
    public const int Max = 50;
    public string Surname { get; set; }
    public string Name { get; set; }
    public string Group { get; set; }
    public int AmountOfMarks { get; set; }
    public int[] Marks { get; set; }

    public StudentMarks(string surname, string name, string group, int amountOfMarks, int[] marks)
    {
        Surname = surname;
        Name = name;
        Group = group;
        AmountOfMarks = amountOfMarks;
        Marks = marks;
    }
}

问题是,我无法阅读&#34; Marks&#34;,因为我不知道如何在Class中初始化数组。我的方法应该运行正常,我不能将marks[i]放入Students[i].Marks[y]

1 个答案:

答案 0 :(得分:0)

我不明白你为什么要这样做。

但是你想要吗?

class StudentMarks 
{ 
  public const int Max = 50; 
  public string Surname { get; set; } 
  public string Name { get; set; } 
  public string Group { get; set; }                                                                       
  public int[] Marks { get; private set; } 

  public StudentMarks(string surname, string name, string group, int amountOfMarks) 
  { 
    Surname = surname;                                                                                    
    Name = name; 
    Group = group; 
    Marks = new int[amountOfMarks]; 
  } 
} 

public static void ReadData(out StudentMarks[] Students, out int amount) 
{ 
  amount = 0; 
  Students = new StudentMarks[Max]; 
  using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P3\\P3.2\\StudentsMarks.csv")) 
  { 
    reader.ReadLine(); reader.ReadLine(); 
    string line = null; 
    while (null != (line = reader.ReadLine())) 
    { 
      string[] values = line.Split(','); 
      string surname = values[0]; 
      string name = values[1]; 
      string group = values[2]; 
      int i = 0; int yMax = 3 + amountOfMarks;int yMin = 4; 
      Students[amount]  = new StudentMarks(surname, name, group, amountOfMark); 
      while (amountOfMarks >= i) 
      { 
        if (yMin <= yMax) 
        { 
          Students[amount].Marks[i] = int.Parse(values[yMin]); 
          yMin++; 
        } 
        i++; 
      } 
      amount++; 
    } 
  } 
}