0

我正在使用 IList List 属性来获取大学课程中的学生课程列表。当我尝试在系类中访问此列表时,该列表是大学课程的一部分。列表为空 ..任何人都可以说出原因吗?

namespace UniversitySystem
{
  class University : CollectionBase
  {
    private string _uniName;
    Departments _depart = new Departments();


    public University(string un, string dn, string cp, List<Student> Slist)
    {
        this._uniName = un;
        this.CED.DepartName = dn;
        this.CED.ChairPerson = cp;
        foreach (Student s in Slist)
        {
            List.Add(s);
        }
    }

    #region properties

    public string UniName
    {
        get { return _uniName; }
        set { _uniName = value; }
    }

    public Departments CED
    {
        get { return _depart; }
        set { _depart = value; }
    }

    #endregion


    public class Departments : CollectionBase
    {
        private string _departName;

        public string DepartName
        {
            get { return _departName; }
            set { _departName = value; }
        }
        private string _chairPerson;

        public string ChairPerson
        {
            get { return _chairPerson; }
            set { _chairPerson = value; }
        }
        public List<Student> StudentListForCED = new List<Student>();


        public Departments()
        {
            _departName = null;
            _chairPerson = null;
            StudentListForCED = null;
        }

        public Departments(string dn, string cp, List<Student> Slist)
        {
            this._departName = dn;
            this._chairPerson = cp;
            foreach (Student s in Slist)
            {
                List.Add(s);
            }
        }

        public void showDetails()
        {
            Console.WriteLine("Departmental Info");
            Console.WriteLine("DepartmentName: " + _departName);
            Console.WriteLine("Chairperson Name: " + _chairPerson);
            Console.WriteLine("Student Info:");
            foreach (Student item in List)
            {
                Console.WriteLine("Name: " + item.Name);
                Console.WriteLine("deptName: " + item.RegNo);
                Console.WriteLine("total marks:  " + item.TotalMarksObtained);
                Console.WriteLine("percentage:   " + item.GetPercentage());
            }
        }
    }
  }
}

main() 部分是

namespace UniversitySystem
{
  class Program
  {
    static void Main(string[] args)
    {

        List<Student> slist = new List<Student>();
        slist.Add(new Student("sana", 1234, "CIS", 650));
        slist.Add(new Student("anam", 2345, "BCIT", 400));
        slist.Add(new Student("fizzza", 2670, "Electrical", 670));


        University u1 = new University("NED", "computer", "UKP", slist);
        Console.WriteLine(u1.CED.DepartName);
        u1.CED.showDetails();



    }
  }
}
4

2 回答 2

1

我做了一些应该有效的更改。

    public Departments(string dn, string cp, List<Student> Slist)
    {
        this._departName = dn;
        this._chairPerson = cp;
        foreach (Student s in Slist)
        {
            StudentListForCED.Add(s);
        }
    }

    public void showDetails()
    {
        Console.WriteLine("Departmental Info");
        Console.WriteLine("DepartmentName: " + _departName);
        Console.WriteLine("Chairperson Name: " + _chairPerson);
        Console.WriteLine("Student Info:");
        foreach (Student item in StudentListForCED)
        {
            Console.WriteLine("Name: " + item.Name);
            Console.WriteLine("deptName: " + item.RegNo);
            Console.WriteLine("total marks:  " + item.TotalMarksObtained);
            Console.WriteLine("percentage:   " + item.GetPercentage());
        }
    }

请更彻底地阅读此内容。

于 2012-06-05T20:24:50.793 回答
1

University 的初始化代码不会将您提供的列表存储为参数。

尝试将列表存储在部门或大学对象本身的变量中,当您显示其中任何一个对象的详细信息时,您可以引用该列表,即您在创建对象时存储的列表。

希望有帮助!

于 2012-06-05T20:07:25.233 回答