2015-05-09 58 views
0

我试图创建一个名为Student的类,其中包含成员名称,GPA和一个名为CourseRecord的对象数组。我完全迷失在这里,因为我无法在我的课程中使用for循环或while循环来初始化CourseRecord对象。这是我想使用的代码,但我不能使用for循环:使用从xml文档读取的对象数组创建类

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Register 
{ 
    class StudentRecord 
    { 
     public string studentName; 
     public double studentGPA; 
     public CourseRecord[] grades = new CourseRecord[4]; 

     public StudentRecord() 
     { 
     } 

     public StudentRecord(string name, double GPA) 
     { 
      studentName = name; 
      studentGPA = GPA; 
     } 

     for (int i = 0; i < CourseRecord.Length; i++;) 
     { 
     CourseRecord[i] = new CourseRecord; 
     CourseRecord[i].Read; 
     } 


    } 
} 
+0

当你说你不能使用for循环时,你的意思是赋值有一条规则说你不能使用for循环吗? – DWright

+0

Microsoft Visual Studio在类,结构或接口成员声明中给我提供错误“无效标记”。 –

+0

这与您在问题标题中提及的阅读xml有什么关系?我完全不清楚自己想要达到的目标或卡在哪里。 “我完全迷失在这里”不是一个好问题。请参阅[如何提出一个好问题?](http://stackoverflow.com/help/how-to-ask),更具体地说[如何创建一个最小,完整和可验证的示例](http:/ /stackoverflow.com/help/mcve)。然后[编辑]你的问题,并具体说明你想要达到的目标,你认为你可以遵循什么方法,以及你卡在哪里。 – Alex

回答

0

所有这些代码是一个方法的身体,这是造成编译器错误之外:

for (int i = 0; i < CourseRecord.Length; int++;) 
{ 
    CourseRecord[i] = new CourseRecord; 
    CourseRecord[i].Read; 
} 

而且,作为一个评论者说,你想i++,不int++;

0

您的代码不在方法内。此外,你应该使用i++而不是int++,因为它是i你增加价值,int不是值。

而且,我会认为CourseRecord.Read是一种方法,在这种情况下,它应该有后的括号,以实际调用的方法。

public void ReadCourseRecords() 
{ 
    for (int i = 0; i < CourseRecord.Length; i++;) 
    { 
     CourseRecord[i] = new CourseRecord; 
     CourseRecord[i].Read(); 
    } 
} 

如果添加这个方法将你StudentRecord类,然后当你怎么称呼它,它应该读的病程记录。