IList List在我的作文类中为空。为什么?

时间:2012-06-05 19:56:58

标签: list c#-4.0 composition ilist

我正在使用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();



    }
  }
}

2 个答案:

答案 0 :(得分:1)

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

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

希望有所帮助!

答案 1 :(得分: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());
        }
    }

请更详细地阅读this