以树方式排列的数据的自定义类



我正在尝试创建一个自定义类,以以下方式排列数据:-

Departments 
        Section 1
                 Person 1
                 Person 2
        Section 2
        ... etc

下面是我写的代码,但它不像我想象的那样工作:-

    public class Sections
    {
        private List<Section> _Section = new List<Section>();
        public List<Section> Section
        {
            get
            {
                return this._Section;
            }
        }
    }
    //Innehåller alla detailer om varje sektion
    public class Section
    {
        public string Name { get; set; }
        public int Capacity { get; set; }
        public int MinimumStaffing { get; set; }
        public int IdealStaffing { get; set; }
        private List<Person> _Employee = new List<Person>();
        public List<Person> Employee
        {
            get
            {
                return this._Employee;
            }
            set { }
        }
    }
    //Innehåller alla info. om en enskild personal
    public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public int TelephoneNo { get; set; }
        public int Pager { get; set; }
        public int HomePhone { get; set; }
        public string Title { get; set; }
    }

我想做的是:-首先:- 获取第 1 节的名称并将其保存为字符串。第二:- 获取本节中每个人的姓名(第 1 节)。第三:- 将第1节的名称和本节中的人员名单添加到"指定部门"列表中。

以下是我用来实现我想要做的事情的代码(这对我不起作用,有关更多信息,请参见下文)

if (index == 0 || index == node.ChildNodes.Count)
                        {
                            if (node.ChildNodes.Count == 2)
                            {
                                sektionsNamn = node.ChildNodes[0].InnerText;
                                continue;
                            }
                            else if (node.ChildNodes.Count > 2)
                            {
                                Sektion.Employee.Add(new Person() { Name = node.ChildNodes[0].InnerText });
                                index = node.ChildNodes.Count;
                                continue;
                            }
                        }
                        else
                        {
                            Sektioner.Section.Add(new Section() { Name = sektionsNamn, Employee=Sektion.Employee });
                            index = 0;
                        }

问题出在 else 语句上,它返回 0 个计数,即它添加了 Name 属性,但不添加员工列表。

知道如何克服这一点吗?

很抱歉打扰,提前感谢。

Section 类中Employee属性的属性 setter 为空,因此当您像这样调用它时,您不会Sektioner.Section.Add(new Section() { Name = sektionsNamn, Employee=Sektion.Employee }) _Employee分配任何内容。在您的示例中,您有Employee属性的以下代码:

public List<Person> Employee
{
     get
     {
           return this._Employee;
     }
     set { }
}

您应该将其更改为此代码(即实现属性设置器)

public List<Person> Employee
{
       get
       {
            return this._Employee;
       }
       set 
       { 
            this._Employee = value;
       }
}

此外,我建议您在类Section中实现构造函数,该构造函数将在其块中设置字段:

public class Section
    {
        public string Name { get; set; }
        public int Capacity { get; set; }
        public int MinimumStaffing { get; set; }
        public int IdealStaffing { get; set; }
        public Section(string name, List<Person> employee)
        {
            this.Name = name;
            this._Employee = employee;
        }
        public Section()
        {
            this._Employee = new List<Person>(); //if you call the empty constructor, create the list
        }
        private List<Person> _Employee; 
        public List<Person> Employee
        {
            get
            {
                return this._Employee;
            }
            set
            {
                this._Employee = value;
            }
        }
    }

然后,您可以将呼叫更改为:

Sektioner.Section.Add(new Section(sektionsNamn, Sektion.Employee));

我发现这更直接,很少导致类似的错误。

最新更新