【问题标题】:c# List<t> within a classc# List<t> 在一个类中
【发布时间】:2015-05-09 14:23:52
【问题描述】:

这是一个相当简单的问题,我有 3 个类(见下面的代码),分别称为项目、站点和水井。项目类将是一个列表,但我希望它包含一个站点列表,然后站点列表将包含一个井列表。不确定这是否允许。我做了一些搜索,我看到的大多数解决方案都更多地讨论了如何在不知道它将是什么类的情况下创建列表类型。我知道我的会是什么,所以这不是问题。

Just to give this a visual

    list<t> Projects
            list<t> Sites
                    list<t> Wells


namespace EDMNoCost
{
    public partial class Form1 : Form
    {
      public class wells
        {
            public string c_wellname { get; set; }
            public string well_id { get; set; }
            public string site_id { get; set; }


            public wells(string c_wellname, string well_id,string site_id)
            {
                this.c_wellname = c_wellname;
                this.well_id = well_id;
                this.site_id = site_id;
            }
        }
        public class sites
        {
            public string c_wellname { get; set; }
            public string site_id { get; set; }
            public string project_id { get; set; }


            public sites(string c_wellname, string site_id, string project_id)
            {
                this.c_wellname = c_wellname;
                this.site_id = site_id;
                this.project_id = project_id;
            }
        }

        public class projects
        {
            public string project_name { get; set; }
            public string project_id { get; set; }
            public string site_id { get; set; }




            public projects(string project_name, string project_id, string site_id)
            {
                this.project_name = project_name;
                this.project_id = project_id;
                this.site_id = site_id;
            }
        }
        public List<projects> projInfo = new List<projects>();
        public List<sites> siteInfo = new List<sites>();
        public List<wells> well_list = new List<wells>();
    }
 }

【问题讨论】:

  • 您的问题到底是什么?您询问是否可以在站点中拥有水井,但您的代码将它们放置在类之外。它们不是包含类的一部分
  • 只是命名提示:如果对象是项目,则将类命名为Project。 Site、Well 等也一样。大写也很有用。

标签: c# list class


【解决方案1】:

拥有这种结构并关闭是合法的,您需要为项目和站点添加 1 个属性,然后您需要更改每个构造函数来初始化属性

完成后,您可以像这样使用您的代码;

2 个项目

第一个有两个站点

第二个有 1 个站点和 1 个井

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {        
        static void Main(string[] args)
        {
            List<projects> projInfo = new List<projects>();

            projInfo.Add(new projects("1", "1", "1"));
            projInfo.Add(new projects("2", "2", "2"));

            projInfo[0].siteInfo.Add(new sites("10", "10", "10"));
            projInfo[0].siteInfo.Add(new sites("20", "20", "20"));

            projInfo[1].siteInfo.Add(new sites("30", "30", "30"));

            projInfo[1].siteInfo[0].well_list.Add(new wells("100", "100", "100"));
        }
    }

    public class wells
        {
            public string c_wellname { get; set; }
            public string well_id { get; set; }
            public string site_id { get; set; }

            public wells(string c_wellname, string well_id,string site_id)
            {
                this.c_wellname = c_wellname;
                this.well_id = well_id;
                this.site_id = site_id;
            }
        }
        public class sites
        {
            public string c_wellname { get; set; }
            public string site_id { get; set; }
            public string project_id { get; set; }
            public List<wells> well_list { get; set; } //new property

            public sites(string c_wellname, string site_id, string project_id)
            {
                this.c_wellname = c_wellname;
                this.site_id = site_id;
                this.project_id = project_id;
                well_list = new List<wells>(); //init the property
            }
        }

        public class projects
        {
            public string project_name { get; set; }
            public string project_id { get; set; }
            public string site_id { get; set; }
            public List<sites> siteInfo { get; set; } //new property

            public projects(string project_name, string project_id, string site_id)
            {
                this.project_name = project_name;
                this.project_id = project_id;
                this.site_id = site_id;
                siteInfo = new List<sites>(); //init the property
            }
        }

}

【讨论】:

    【解决方案2】:

    这是完全合法的,只需将它们添加为公共属性:

    public class projects
    {
        public List<sites> siteInfo { get; set; }
        ...
        public projects (string project_name, string project_id, string site_id)
        {
            this.project_name = project_name;
            this.project_id = project_id;
            this.site_id = site_id;
            this.siteInfo = new List<sites>(); // You can add stuff to the List here if you want, or you can just initialize it.
        }
    }
    

    你也可以为你的其他班级做同样的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多